How to pass a view model in custom event args - c#

I use the Caliburn Micro framework. That doesn't actually matter. The point is, that I publish an event in a view model, which contains the new view model to be shown in its event args. The event gets catched in the ShellViewModel (you could see it as the root view model), which actually activates the new view model.
So how could I pass the view model in my event args? Currently it looks like this:
// where it gets published; "AnotherViewModel" is the actual class
public void AMethod()
{
var args = new ViewModelChangedEventArgs { ViewModelType = typeof(AnotherViewModel) };
PublishEvent(args);
}
// event handler
public void Handle(ViewModelChangedEventArgs message)
{
if (message.ViewModelType == typeof(AnotherViewModel))
{
// activate AnotherViewModel
}
if (message.ViewModelType == typeof(FooViewModel))
{
// activate FooViewModel
}
}
This method seems not very elegant to me. Do you have any better ideas?

Overall solution is pretty well, you just passing a meta information in the event args which is enough to create a new ViewModel. Regarding ViewModel creating itself, this is a standard design problem which is solved by implementing Factory pattern. Basically you need a factory which is able creating concrete ViewModel by a type, so your handler code would be like below:
public void Handle(ViewModelChangedEventArgs message)
{
var viewModel = viewModelFactory.Create(typeof(AnotherViewModel));
}

Related

How to properly notify view layer to open a dialog?

I have a WPF application and i'm trying to respect the MVVM pattern rules. One of my views contains button:
<Button
Command="{Binding BrowseCommand}"
Margin="50, 0, 0, 0"
Style="{StaticResource CommonButtonStyle}"
Width="100"
Height="30">
<TextBlock
Text="Browse"/>
</Button>
Button command calls a method:
private void Browse(object sender)
{
DialogService.BrowseForDestinationPath(DestinationPath);
}
The main purpose of this method is to show "Select-directory-dialog", collect data and return it to the view model.
public static class DialogService
{
public static event Action<string> FolderBrowseRequested;
...
public static void BrowseForDestinationPath(string initialPath)
{
FolderBrowseRequested?.Invoke(initialPath);
}
}
Event defined in my DialogService class is invoked, and the subscriber method located in code-behind of the dialog fires:
protected void OnFolderBrowseRequested(string initialPath)
{
string destinationPath = initialPath;
var browsingDialog = new VistaFolderBrowserDialog();
if(browsingDialog.ShowDialog(this).GetValueOrDefault())
{
destinationPath = browsingDialog.SelectedPath;
var dataContext = DataContext as UnpackArchiveWindowViewModel;
if (dataContext != null)
dataContext.DestinationPath = destinationPath;
}
DialogService.FolderBrowseRequested -= OnFolderBrowseRequested; //so dumb
}
The problem is i really don't like this solution, I'm convinced it's unnecessarily complicated and inelegant. How to properly show a dialog on button click, collect some data and deliver it to our view model? I would like to keep View and ViewModel seperated and fully respect MVVM regime.
You could first start by describing the behavior your DialogService needs in an interface.
public interface IDialogService
{
void BrowseForDestinationPath(string initialPath);
event PathSelectedEvent PathSelected;
}
public delegate void PathSelectedEvent(string destinationPath);
Your ViewModel would contain a member of type IDialogService and subscribe to the PathSelectedEvent. The BrowseForDestinationPath method would be called using your Browse method which is called using the Command.
You could then create a user control which implements IDialogService. You could either inject this through your ViewModels constructor or if your ViewModel had a property like
public IDialogService FolderBorwser {get;set;}
the benefit of this approach is that all your view model knows about is an interface. You now delegate the responsibility of creating a concrete instance to something else. I would reccomend using an Injection Container like Unity or MEF as they handle the job of managing and resolving dependencies.
I encourage you to write your own logic because it helps you to understand the problem of opening dialogs in MVVM, but if you hit a brick wall or wan't to take the easy way out, there is a library called MVVM Dialogs that can help you with these problems.
Using this library you would write your code like this.
private void Browse(object sender)
{
var settings = new FolderBrowserDialogSettings
{
Description = "This is a description"
};
bool? success = dialogService.ShowFolderBrowserDialog(this, settings);
if (success == true)
{
// Do something with 'settings.SelectedPath'
}
}

BindableProperty in custom view does not unsubscribe PropertyChanged

Background info
I'm developing a Xamarin Forms (v4.1.1.3, testing on iOS) application in XAML, using MVVM with a View first approach; I'm assigning single-instance ViewModels to Views by using the ViewModelLocator service of MVVMLight:
BindingContext="{Binding [SearchViewModel], Source={StaticResource ViewModelLocator}}"
When navigating to another page, I'm constructing a new instance of the page, which will receive the very same ViewModel instance every time.
var page = new SearchView();
var tabbedPage = Application.Current.MainPage as TabbedPage;
if (tabbedPage != null)
await tabbedPage.CurrentPage.Navigation.PushAsync(page);
The issue
I've implemented a custom control (view?), that is supposed to show search results in a tile-like layout. This control is created when navigating from a search NavigationPage to a search results ContentPage.
Every time I return to the search page and navigate back to search results, the view is reconstructed and the PropertyChanged of the BindableProperties are subscribed. These PropertyChanged events are never unsubscribed, so every time I navigate to the search results view and change the bound ViewModel property, the event is fired increasingly multiple times.
In the following code the OnItemsPropertyChanged is triggered multiple times, based on how many times I've navigated from the search view to the search results view:
public class WrapLayout : Grid
{
public static readonly BindableProperty ItemsProperty =
BindableProperty.Create("Items", typeof(IEnumerable), typeof(WrapLayout), null, propertyChanged: OnItemsPropertyChanged);
public IEnumerable Items
{
get { return (IEnumerable)GetValue(ItemsProperty); }
set { SetValue(ItemsProperty, value); }
}
public WrapLayout()
{
...
}
private static void OnItemsPropertyChanged(BindableObject bindable, object oldValue, object newValue)
{
...
}
}
My questions:
Shouldn't the BindableProperty unsubscribe from PropertyChanged and -Changing by itself?
Does this occur because of the way I associated Views with ViewModels and/or navigate through pages?
Should I handle unsubscribing these events myself, and how?
EDIT; additional navigation info
I have a MainView TabbedPage, which creates SearchView as NavigationPage:
public MainView()
{
InitializeComponent();
Children.Add(new NavigationPage(new SearchView())
{
Title = AppResources.Tab_Search,
Icon = "tab_search"
});
}
SearchView has, upon creation, a single-instance ViewModel assigned by the ViewModelLocator that was mentioned at the start of this topic, using MVVMLight's SimpleIoc container.
When a search command in SearchView is fired, I send a request to an API which returns search results. These results are displayed on another page, to which I navigate to from the SearchView's ViewModel:
await _navigationService.NavigateTo(ViewModelLocator.PageKeyFileResults, searchResult);
Which functionality looks somewhat like this:
public async Task NavigateTo(string pagekey, object viewModelParameter)
{
var constructor = _pagesByKey[pagekey].Constructor; //Gets the Func<Page> that simple creates the requested page, without using reflection.
var page = constructor() as Page;
var viewModel = page.BindingContext as BaseViewModel;
if (viewModel != null)
viewModel.Initialize(viewModelParameter);
var tabbedPage = Application.Current.MainPage as TabbedPage;
if (tabbedPage != null)
await tabbedPage.CurrentPage.Navigation.PushAsync(page);
else
await Application.Current.MainPage.Navigation.PushAsync(page);
}
The constructed page looks somewhat like:
<pages:BaseContentPage
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Views.FileResultsView"
xmlns:pages="clr-namespace:Views.Pages;assembly=Views"
xmlns:controls="clr-namespace:Views.Controls;assembly=Views"
BindingContext="{Binding [FileResultsViewModel], Source={StaticResource ViewModelLocator}}">
<ScrollView>
<controls:WrapLayout
Items="{Binding SearchResults}" />
</ScrollView>
</pages:BaseContentPage>
Where BaseContentPage is:
public class BaseContentPage : ContentPage
{
protected override void OnAppearing()
{
base.OnAppearing();
MessagingCenter.Subscribe<DialogMessage>(this, "ShowDialog", (dialogMessage) =>
{
if (string.IsNullOrWhiteSpace(dialogMessage.AcceptButton))
DisplayAlert(dialogMessage.Title, dialogMessage.Content, dialogMessage.CancelButton);
else
DisplayAlert(dialogMessage.Title, dialogMessage.Content, dialogMessage.AcceptButton, dialogMessage.CancelButton);
});
}
protected override void OnDisappearing()
{
base.OnDisappearing();
MessagingCenter.Unsubscribe<DialogMessage>(this, "ShowDialog");
}
}
And where ViewModel is basically like this:
public class FileResultsViewModel : BaseViewModel
{
private IEnumerable<ASRow> _searchResults;
public IEnumerable<ASRow> SearchResults
{
get { return _searchResults; }
set { Set(ref _searchResults, value); }
}
internal override void Initialize(object parameter)
{
base.Initialize(parameter);
if (parameter is AdvancedSearchResponse)
{
var searchResults = parameter as AdvancedSearchResponse;
SearchResults = new List<ASRow>(searchResults.Rows);
}
}
}
Shouldn't the BindableProperty unsubscribe from PropertyChanged and -Changing by itself?
Yes - it should. If it does not it is most certainly a bug
Does this occur because of the way I associated Views with ViewModels and/or navigate trough pages?
That is most likely also an option, since i didn't experience the behaviour you described yet. You would need to share more of your surrounding setup code.
Should I handle unsubscribing these events myself, and how?
It's hard for you to always control unsubscribing, since most of the time it will be the control subscribing to events (unless you do it yourself, in which case it's always your duty to unsub again)
While it is ugly it's sometimes necessary to get a quick workaround, which in your case would be browsing how xamarin holds a list of the change delegates and manually unsubscribe them on page appearing for example.
I hope that answers your question. Feel free to comment if it does not.
Update
In your case i would debug your page base, and verify wether or not
OnDisappearing is called correctly
Your handler is gone after unsubscribe
(This is lazy but i usually unsub an event before subbing it, just to make sure such a bug does not happen, because most EventManagement services won't throw if you're trying to unsub a handler which is not registered.)
at least that's the most likely causes of your issue.
Shouldn't the BindableProperty unsubscribe from PropertyChanged and -Changing by itself?
No. The Binding class takes care of this. Not the BindableProperty.
Does this occur because of the way I associated Views with ViewModels and/or navigate through pages?
You are seeing this because you are forgetting that the Navigation Stack keeps a list of pages in memory. Since multiple pages are pointing to the same BindingContext, there are multiple observers to changes. You would not have this particular issue if you didn't re-use View Models.
Should I handle unsubscribing these events myself, and how?
No. If it is really a concern then set BindingContext to null when a page disappears, and then restore it when reappearing. Keep in mind though that this still has cost to it, especially if your UI is really busy and has lots of dynamic content that is controlled by data bindings.

Do functions when closing and when changing ViewModel - C# using MVVM

I have some questions and I'm having some trouble finding answers so I decided to put them here.
Q1: I have to make to call a function each time the app closes, like: click exit button and then do something.
Q2: I have a menuitemcontrol in the shell viewmodel that controls the ViewModel but on creating them I do some webservices requests, but imagine I delete a friend it is requested that I update the request in the viewmodel, how can I do this calling from other viewmodels?
EDIT: Scenario - ShellViewModel that contains HomeViewModel and FriendsViewModel, I accepted a friend in the FriendsViewModel I want that when I click Home the function that fetch the info from the webservice to run again. (If I was doing in code-behind I would use Onclick[Home] > runlogin())
UpdateQ2:
public FriendsViewModel()
{
MessengerInstance.Register<NotificationMessage>(this, NotifyMe);
au = AuthSingleton.Instance.getAuthUser(); // Singleton that works like a session in the desktop App.
if (AuthSingleton.Instance.IsAuth == true)
loadFriends();
}
public void NotifyMe(NotificationMessage notificationMessage)
{
string notification = notificationMessage.Notification;
//do your work
loadFriends();
}
#endregion constructors
public async void loadFriends()
{
var response = await CommunicationWebServices.GetASM(au.idUser + "/friends", au.token);
var fh = JsonConvert.DeserializeObject<FriendsHandler>(response);
}
I've decided to use a suggestion from a commenter user to send a message from the second ViewModel to this one to order the update to run again (pretty cool and easy solution), but it doesn't work because somehow my singleton is deleted :O
Message sent: MessengerInstance.Send(new NotificationMessage("notification message"));
Best regards,
Q1 - What MVVM framework are you using? All MVVM frameworks I know implement custom commands (aka RelayCommands/DelegatingCommands), so you could attach them to Window events. Another solution would have in your ViewModel an implementation of ClosingRequest event. Something like this:
public class BaseViewModel
{
public event EventHandler ClosingRequest;
protected void OnClosingRequest()
{
if (this.ClosingRequest != null)
{
this.ClosingRequest(this, EventArgs.Empty);
}
}
}
So, in your View you would have:
public partial class MainWindow: Window
{
...
var vm = new BaseViewModel();
this.Datacontext = vm;
vm.ClosingRequest += (sender, e) => this.Close();
}
If you are using MVVM Light, you can do the following in your ViewModel:
public ICommand CmdWindowClosing
{
get
{
return new RelayCommand<CancelEventArgs>(
(args) =>{
});
}
}
And in your Window:
<i:Interaction.Triggers>
<i:EventTrigger EventName="Closing">
<command:EventToCommand Command="{Binding CmdWindowClosing}" PassEventArgsToCommand="True" />
</i:EventTrigger>
</i:Interaction.Triggers>
Q2 - Also, this is easy when using a MVVM framework. Most of them implement the Message Mediator pattern. What this mean for your. This mean you can dispatch a message warning "Request needs update" and a receptor binds to that message, implementing something when the message is received. Take a look on this demo from Microsoft

View is not locating the Viewmodel with default VML in MVVMCross

I'm trying to create a Portable Class such that I can use that across the platforms. It is working fine in Windows Phone 8.1 App. But when it comes to Android, then it is showing the Viewmodel as null and DataContext as Null in debugger which breaks the application debugger. When I create another viewmodel and view to test the app, its working fine on android too. What can be the possible reasons.
EDIT : It is crashing due to the constructor , in which I am passing the business Logic instance. So , Constructor is necessary i think but in that case it is crashing.I am not trying to resolve the ViewModel , i am trying to resolve the Service instance in ViewModel and for the purpose of MVVM, I am keeping the Service out from Droid Project so base.OnCreate(bundle) does not come into scene anyways.
public BookViewModel(ILogic _logic)
{
logic = _logic;
//var ss= Mvx.Resolve<ILogic>();
//var x = Mvx.CanResolve<ILogic>();
_details = logic.Read();
}
Below is the Logic Code :
public class Logic : ILogic
{
#region Attributes
List<Detail.Detail> _details = new List<Detail.Detail>();
DataLayer.DataLayer dl = new DataLayer.DataLayer();
#endregion
#region .ctor
public Logic()
{
populateList();
}
#endregion
#region Methods
private void populateList()
{
_details = dl.Access();
}
Below is the App.cs in ViewModel in which CanResolve is giving False
public class App : Cirrious.MvvmCross.ViewModels.MvxApplication
{
#region Methods
public override void Initialize()
{
Mvx.RegisterType<ILogic, Logic>();
var ss = Mvx.CanResolve<ILogic>();
RegisterAppStart<ViewModels.BookViewModel>();
}
#endregion
}
There are a few questions and answers around similar to this - e.g. similar to MVVMCross ViewModel construction failure notifications
The basic answer is that MvvmCross cannot resolve the ViewModel during the constructor - you have to wait until after the base.OnCreate(bundle) call - at this point the ViewModel will be resolved.
There's also a bit more about when ViewModel's are located in Who should create view model instances in MvvmCross and CoreDispatcher.HasThreadAccess "breaking change" (and probably a few other places too)

MVP pattern on .net

I want to understand on how to implement a MVP pattern on .net using windows form.
In the future I want to use the created pattern on web.
My problem is that I'm not sure if I do it right.
What I did is some presenter I attach multiple view to it, which means that I cannot used that presenter without specifying first all the views that are attached to it.
public class ScorePresenter{
private IScoreView _scoreView;
private IClientView _clientView;
public ScorePresenter()
{
}
public void AttachView(IScoreView view){
this._scoreView = view;
}
public void AttachView(IClientView view){
this._clientView = view;
}
public void Create(Model model){
try{
//create code here
this._clientView.Reload();
}
catch(Exception ex){
}
}
}
public class ClientPresenter(){
private IClientView _clientView;
public ClientPresenter(){
}
public void AttachView(IClientView view){
this._clientView = view;
}
}
public interface IClientView{
void Reload();
}
public interface IScoreView{
}
usage
client form vb.net
Public Class ClientForm
Implements IClientView
Private _clientPresenter As ClientPresenter
Public Sub ClientForm_Load() Handles Me.Load
Me._clientPresenter = new ClientPresenter()
Me._clientPresenter.AttachView(Me)
End Sub
Public Sub Reload Implements IClientView.Reload
Reload code here
End Sub
Public Sub ScoreButton_Click() Handles ScoreButton.Click
Dim frmScoreForm as New ScoreForm
frmScoreForm.MyParent = Me
frmScoreForm.ShowDialog()
End Sub
End Class
score form vb.net
Public Class ScoreForm
Implements IScoreView
Private _scorePresenter As ScorePresenter
Public Sub ScoreForm_Load() Handles Me.Load
Me._scorePresenter = new ScorePresenter()
Me._scorePresenter.AttachView(Me)
Me._scorePresenter.AttachView(Me._myParent)
End Class
Private _myParent as Object
Public WriteOnly Property MyParent As Object
Set(value As Object)
Me._myParent = value
End Set
End Property
End Class
on this code client form is the main form if i clicked the score button on client form it will show the score form
on score form if i create or manipulate data in it it will call the client form reload
and client form will also update it's data on the view
what I see in this one is that I cannot use ScorePresenter alone right? Is that a bad design? if yes is there other way to achieve what I want to happen?
If I understand your issue correctly, you want to communicate views in a sense that changes in one of them should update the other.
If this is the case, your approach is wrong.
First, your presenter should not manage both views. A reasonable rule in mvp is to have a presenter for each view so that you have 1-1 correspondence between views and presenters.
Then, the communication between presenters is done with messaging, for example with the Event Aggregator. Presenters subscribe to events and other presenters publish events. This way your presenters are completely decoupled from each other, instead they are only coupled to the eventing engine. And publications-subscriptions let you create implicit dependencies.
In other words, if the data in one view changes, the view uses its presenter to raise an event. Some other subscribing presenters catch the event and call updating methods on their views.
In your specific case you should
introduce another presenter, the ClientPresenter
learn an existing or create a custom implementation of an event aggregator
introduce the event aggreegator in both presenters
create event classes and wire up subscriptions in presenters
raise events when appropriate in one of your presenters

Categories