Call a method from a child view - c#

I'm trying to make some changes to the code a colleague made.
So, I have a ShellView that loads documents (and shows them as its content) with a method defined in its ViewModel, and the child view, a StatusBarView which holds the path navigated in the documents and some other infos.
public class ShellViewModel
{
public StatusBarViewModel StatusBar { get; }
public ShellViewModel(StatusBarViewModel statusBarViewModel, ...)
{
StatusBar = statusBarViewModel;
var keymap = new Keymap();
keymap.Map("F2", new SimpleCommand("open-file",
"Shows the open file dialog",
param => OpenFile());
}
private void OpenFile()
{
// Logic to open the file that uses other methods
// inside this VM to validate the file
}
}
At the moment you can load a new pack of documents pressing a key, I'd like to do the same with a button in the Status Bar and calling that method.
What is the proper way to call a method existing in the parent view from the child view?

In your child view Define an event Handler
public EventHandler OpenFileHandler
On the click of the button of your Status Bar view do this:
public Btn_Click(object sender, RoutedEventArgs e)
{
OpenFileHandler(this, e);
}
in your parent view, when you create your status bar view, define the delegate
statusbar.OpenFileHandler+= delegate
{
ShellViewModel instance = this.DataContext as ShellViewModel;
instance.OpenFile();
}
statusbar is the name i gave to your status bar view, but it represent the instance of it

There are many ways. First need to analyze your purpose.
- You can create an ActionEvent or EventHandler inside childview and on button click you can raise that event.
- Another way use can use Mediator pattern/Observer pattern
Example:
Inside child ViewModel:
public event EventHandler openFileEvent;
Inside click button action on status bar:
private void btnClick()
{
if(null != openFileEvent)
{
openFileEvent(this, new EventArgs{});
}
}
Inside Parent ViewModel:
statusBarViewModel.openFileEvent += new EventHandler(EventHandlerName);
private void EventHandlerName(objehct sender, EventArgs...)
{
...
OpenFile();
...
}

Related

Detecting tab changed event

I am working on xamarin.forms. I need to detect the event of tab being changed either by swapping left or right or by clicking without using custom renderer
I tried below event but it is firing in both cases when child page being pushed or tab being changed. how can i get isolated event of tab being changed
public class MyTabbedPage : TabbedPage
{
public MyTabbedPage()
{
this.CurrentPageChanged += CurrentPageHasChanged;
}
protected void CurrentPageHasChanged(object sender,EventArgs e)
{
var pages= Navigation.NavigationStack;
if (pages.Count > 0)
{
this.Title = pages[pages.Count - 1].Title;
}
else
this.Title = this.CurrentPage.Title;
}
}
This issue I am facing is: In below screenshot part1 is Homepage(title="Diary") & part2 is Childpage(title="Homework") when I change tab & again come to first tab than navigationbar title getting changed "Homework" to "Diary"(Screeshot2)
As you are on your tabbed page already you can literally just do the following
public partial class MyTabbedPage : TabbedPage
{
public MyTabbedPage()
{
InitializeComponent();
CurrentPageChanged += CurrentPageHasChanged;
}
private void CurrentPageHasChanged(object sender, EventArgs e) => Title = CurrentPage.Title;
}
if you want to use the sender you can do the following
public partial class MyTabbedPage : TabbedPage
{
public MyTabbedPage()
{
InitializeComponent();
CurrentPageChanged += CurrentPageHasChanged;
}
private void CurrentPageHasChanged(object sender, EventArgs e)
{
var tabbedPage = (TabbedPage) sender;
Title = tabbedPage.CurrentPage.Title;
}
}
Or if you can elaborate on what you are trying to do exactly I can give a better answer as I do not know what it is that you are trying to do exactly
I don't think you can achieve what you want to do, at least not like this. The event behaves the way it does and as described: it is fired whenever the current page changes, also for children.
That being said, I think you should focus on implementing the functionality you want with the tools we have. I can't really deduce from your code what you are trying to do, but it looks like you want to change the title? When a tab is changed? Why not just make some kind of condition to only do it for certain pages? For example when the page is contained in the Children collection?

UITableView to ObservableCollection binding breaks when the containing UIViewController is initialised for the second time

I'm using mvvmcross and xamarin to bind an ObservableCollection to a UITableView. The collection is updated in place using the Add, Remove and Move methods. These calls correctly trigger INotifyCollectionChanged events and the TableView is updated as expected the first time the view containing the table is shown. If the user navigates away from the original view as part of the normal application flow but later returns the correct data is loaded into the table but calls to add, move and remove no longer update the table.
The INotifyCollectionChanged events are still being fired when the collection is updated
If I manually subscribe to these events in my subclass of MvxStandardTableViewSource and try and call ReloadData on the UITableView still does not update
My presenter is creating a new instance of the viewmodel and view each time the page is visited.
I'm also using Xamarin-Sidebar (https://components.xamarin.com/view/sidebarnavigation) for navigation in my application with a custom presenter to load the views but as far as I can tell the view is initialised via exactly the same code path whether it's the first or subsequent visit.
My presenters Show() method looks like this:
public override void Show(MvxViewModelRequest request)
{
if (request.PresentationValues != null)
{
if(NavigationFactory.CheckNavigationMode(request.PresentationValues, NavigationFactory.ClearStack))
{
MasterNavigationController.ViewControllers = new UIViewController[0];
base.Show(request);
}
else if(NavigationFactory.CheckNavigationMode(request.PresentationValues, NavigationFactory.LoadView))
{
var root = MasterNavigationController.TopViewController as RootViewController;
var view = this.CreateViewControllerFor(request) as UIViewController;
root.SidebarController.ChangeContentView(view);
}
}
else
{
base.Show(request);
}
}
The binding in my ViewController looks like this:
public override void ViewDidLoad()
{
base.ViewDidLoad();
View.AutoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight;
var source = new TracksTableSource(TableView, "TitleText Title; ImageUrl ImageUrl", ViewModel);
TableView.Source = source;
var set = this.CreateBindingSet<TracksViewController, TracksViewModel>();
set.Bind(source).To(vm => vm.PlaylistTable);
set.Apply();
}
And my viewmodel is as below where PlaylistTable is a subclass of ObservableCollection with the Update method using add, move and remove to keep the collection up to date.
public class TracksViewModel : MvxViewModel
{
private readonly IPlaylistService _playlistService;
private readonly IMessengerService _messengerService;
private readonly MvxSubscriptionToken _playlistToken;
public PlaylistTable PlaylistTable { get; set; }
public TracksViewModel(IPlaylistService playlistService, IMessengerService messengerService)
{
_playlistService = playlistService;
_messengerService = messengerService;
if (!messengerService.IsSubscribed<PlaylistUpdateMessage>(GetType().Name))
_playlistToken = _messengerService.Subscribe<PlaylistUpdateMessage>(OnDirtyPlaylist, GetType().Name);
}
public void Init(NavigationParameters parameters)
{
PlaylistTable = new PlaylistTable(parameters.PlaylistId);
UpdatePlaylist(parameters.PlaylistId);
}
public async void UpdatePlaylist(Guid playlistId)
{
var response = await _playlistService.Get(playlistId);
PlaylistTable.Update(new Playlist(response));
}
private void OnDirtyPlaylist(PlaylistUpdateMessage message)
{
UpdatePlaylist(message.PlaylistId);
}
}
This setup works perfectly the first time the view is initialised and updates the table correctly, it's only the second and subsequent times the view is initialised that the table fails to update. Can anyone explain why the binding fails when it appears the view is created using the same techniques in both instances?
I can post additional code if required but I believe the issue will be how I'm using the presenter since the code I've not posted from PlaylistTable functions correctly in unit tests and on first viewing.

Raise an event from Child View Model

I have a setup as follows.
ContainerViewModel
SearchViewModel
ResultsViewModel
Thats because I wan't to use the SearchView and ResultsView in different parts of my application
My ContainerViewModel has a handle to the other VM's like
SearchViewModel searchbViewModel = new SearchbViewModel();
ResultsViewModel resultsViewModel = new ResultsViewModel();
Each View Model has their own DataContext
I want to be able to raise an event from the SearchViewModel to the ContainerViewModel to let it know a search has been performed.
This is what I have tried:
ContainerViewModel
searchJobViewModel.OnSearchPerformed += SearchJobViewModel_OnSearchPerformed;
public void SearchJobViewModel_OnSearchPerformed()
{
}
SearchViewModel
public delegate void SearchPerformed();
public SearchPerformed OnSearchPerformed { get; set; }
public void Execute_SearchJobs()
{
if (OnSearchPerformed != null)
OnSearchPerformed();
}
When I hit the search button and the Execute_SearchJobs method fires OnSearchPerformed is always null
What am I missing?
Does that even compile? I think what you want is an event:
public event SearchPerformed OnSearchPerformed;
Why your eventhandler is null is probably because the code that added a receiver to the event was not called yet or was called on a different instance of the class. You will need to debug that behaviour or post more code here.

List+Detail - Best Approach?

I hope you guys can help me out as I can't find anything useful that helps with the understanding of my problem:
I'm trying to realize a passive MVP approach on my C# WinForms application which has list views and corresponding detail views.
So far I've got the following structure (pseudo code):
ListPresenter(new Repository(), new ListView(), new DetailPresenter(new DetailView());
Implementation:
public class UserDetailPresenter : IPresenter<IUserDetailView>
{
private IDetailView _view;
public UserDetailPresenter(IDetailView detailView)
{
_view = detailView;
}
public void Show(IUser user)
{
InitializeView(user);
_view.Show();
}
}
public class UserListPresenter
{
//private members (_userRepo, _listView, _detailPresenter)
public UserListView(IUserRepository userRepo, IListView listView, IDetailPresenter detailPresenter)
{
//wire up private members..
_listView.EditCommandFired += this.ShowEditForm;
}
private void OnListViewEditCommandFired(object sender, EventArgs args)
{
_detailPresenter.LoadUser(_listView.SelectedUser);
_detailPresenter.Show(); //modal
}
}
public class UserListForm : Form, IUserListView
{
public event EventHandler EditCommandFired;
public IUser SelectedUser { get { return gridView.FocusedRowHandle as IUser; } }
public void LoadUsers(List<IUser> users)
{
gridView.DataSource = users;
}
// other UI stuff
}
My problem is: I can only show the edit form once. As soon as I try to open it for a second time my View (the form) is disposed (System.ObjectDisposedException).
How do I fix that? Do I have the wrong approach here? Do I either cancel the form's close and just hide it and trust the garbage collector to collect it once the DetailPresenter is disposed? Do I create (new() up) a new presenter each time the Edit event is fired? I would then have to introduce some kind of factory as I somehow lose dependency injection. I'd appreaciate if someone could point out how the best practice in this case would look like and what I may be doing wrong here..
I was doing Winforms MVP a while ago so not sure if I can help, but the case my be as follows.
In my approach, the view was owning presenter, pseudo code:
MyForm form = new MyForm(new PresenterX);
form.Show(); //or showdialog
In this case instance is still there after closing.
In your case since presenter owns the view, its possible that once presenter is not used, GC disposes presenter and contained view.
Or even if presenter is still in use, since view is private GC may collect it once closed.
Try to debug in Release mode and see what happens with closed form instance.
EDIT:
Other idea is:
Create instance of view first and then pass to presenter
So approach that may fail (I don' see full code so guessing)
UserDetailPresenter p = new UserDetailPresenter(new YourView());
Try
YourForm view = new YourForm(); //as global variable, view should be reusable anyway
Somewhere in code
UserDetailPresenter p = new UserDetailPresenter(view);
p.Show(userInstance);
You're using one instance of DetailPresenter to show details for different objects. So you'll have to initialize the view of the DetailPresenter each time you want to show it, in your current implementation. This could be one way of doing it, the ListPresenter can inject a new instance of DetailsView everytime it asks the DetailPresenter to show it.
public class UserDetailPresenter : IPresenter<IUserDetailView>
{
private IDetailView _view;
public UserDetailPresenter()
{
}
public void Show(IUser user, IDetailView detailView)
{
_view = detailView;
InitializeView(user);
_view.Show();
}
}
Or another cleaner way could be some sort of ViewFactory to get a new instance of the view before showing it.
private IDetailViewFactory _detailViewFactory;
public UserDetailPresenter(IDetailViewFactory detailViewFactory)
{
_detailViewFactory = detailViewFactory;
}
public void Show(IUser user )
{
_view = _detailViewFactory.Resolve();//Some method to get a new view
InitializeView(user);
_view.Show();
}
But if you want to do it a bit differently, this is more passive view way.
In the ListPresenter:
private void OnListViewEditCommandFired(object sender, EventArgs args)
{
_listView.Show(_listView.SelectedUser);//tells view to show another view
}
In the ListView:
public ListView()
{
new ListPresenter(this); // initializes presenter
}
public void Show(IUser user)
{
new DetailsView(user); // creates a new view
}
In the DetailsView:
public DetailsView(IUser user)
{
new DetailsPresenter(this, user); //creates presenter
}
Finally:
public class UserDetailPresenter : IPresenter<IUserDetailView>
{
private IDetailView _view;
public UserDetailPresenter(IDetailView detailView, IUser user)
{
_view = detailView;
LoadUser(user);
_view.SomeProperty = _userData;//to populate view with data
_view.Show(); // tells the view to show data
}
}

Mvp View knows Model

I'm trying to use MVP and I notice that my View must know Model that should not happen in MVP I presume.
here is example:
public partial class TestForm : Form, ITestView
{
public void LoadList(IEnumerable<AppSignature> data)
{
testPresenterBindingSource.DataSource = data;
}
}
public interface ITestView
{
event EventHandler<EventArgs> Load;
void LoadList(IEnumerable<AppSignature> data);
}
public class TestPresenter
{
private ITestView view;
public TestPresenter(ITestView view)
{
this.view = view;
view.Load += View_Load;
}
private void View_Load(object sender, EventArgs e)
{
var data = // get from model
view.LoadList(data);
}
}
and the problem is that in TestForm I need reference to AppSignature.
In all tutorials I saw, there are some simple examples like
public void LoadList(IEnumerable<String> data) where there is no need reference to model. But how i.e DataGridView can publish current row data?
Your form is a View, it is not a Presenter. Thus it should implement interface ITestView:
public interface ITestView
{
event EventHandler Load;
void LoadList(IEnumerable<AppSignatureDto> data);
}
And your Presenter is someone, who subscribes to view's events and uses view properties to read and update view:
public class TestPresenter
{
private ITestView view;
public TestPresenter(ITestView view)
{
this.view = view;
view.Load += View_Load;
}
private void View_Load(object sender, EventArgs e)
{
List<AppSignature> signatures = // get from model
List<AppSignatureDto> signatureDtos = // map domain class to dto
view.LoadList(signatureDtos);
}
}
And you form, as I already said, is a view, it does not know anything about presenter and model:
public partial class TestForm : Form, ITestView
{
public event EventHandler Load;
private void ButtonLoad_Click(object sender, EventArgs e)
{
if (Load != null)
Load(this, EventArgs.Empty);
}
public void LoadList(IEnumerable<AppSignatureDto> data)
{
// populate grid view here
}
}
How to deal with reference to domain classes? Usually I provide to view only simple data (strings, integers, dates, etc), or I create data transfer objects, which are passed to view (you can name them FooView, FooDto, etc). You can easily map them with something like AtoMapper:
List<AppSignatureDto> signatureDtos =
Mapper.Map<List<AppSignature>, List<AppSignatureDto>>(signatures);
The View may have knowledge of Model as long as the interaction is limited to data binding only. i.e. View should not try to manipulate Model directly. View will always redirect user input to Presenter and Presenter will take care of further actions. If any action performed by Presenter results in a change in state of Model, Model will notify View via data binding. Model will be completely unaware of existence of View.
Is it OK to get DataSource in Presenter and there set its DataSource ?
e.g.
Presenter code:
Public void LoadData()
{
_view.Data.DataSource = Business.GetData().ToList();
}
Form code:
Public BindingSource Data
{
get
{
return this.bsData;
}
}
Thanks to that I dont need to add any references to the View, but I didn't see that solution in any other sources.

Categories

Resources