Hi,
In our sample application (by using MVVM Pattern), MessagingCenter is not subscribing to the items after unsubscribing and Popasyn()/Harward backkey press in page2.
page1.Xaml.cs
MessagingCenter.Subscribe(this, "displayDialog_Page1_loading", message => {
Device.BeginInvokeOnMainThread(() =>
{
DependencyService.Get().Show("loading");
});
});
protected override void OnDisappearing() { base.OnDisappearing(); MessagingCenter.Unsubscribe<string>(this, "displayDialog_Page1_loading"); //MessagingCenter.Unsubscribe<string>(this, "displayDialog_Configuration"); }
Page1ViewModel.cs
MessagingCenter.Send<String>("Start", "displayDialog_Page1_loading"); await Task.Run(async () => { // Application functions }
2.Page2:-
Navigation.PopAsync(false);
"After PopAsync(false)/ hardware back key press ", the message center is not subscribing to the item to show the progress dialog from page 1.
Kindly advise on this .
Answers
@KathirVel - where are you putting the Subscribe functions, are they placed in the OnAppearing function or in the constructor?
Also if you are just after getting the OnAppearing and OnDisappearing into the ViewModel, I would suggest having a look at this BasePage https://github.com/adamped/xarch-starter/blob/master/Mobile/Base/BasePage.xaml.cs
and the BaseViewModel. It may be an easier way to implement everything.
In MVVM messenger is designed for communication between ViewModels, not between ViewModel and Views.
@KathirVel
It's not clear from your sample what your trying to achieve but when you call Page2 OnDisappearing will fire on page1 and your message listener will be unsubscribed so when you go back to page1 nothing will happen because the listener has been unsubscribed.
The safest way to achieve this is to instantiate listeners in Page and VM ctor's and unsubscribe from the message when the page is popped. OnDisappearing is not a good approach for cleanup and it fires when navigating to another page.
I use a custom navigation page such as:
Then in the page
This code uses the MVVM Light messenger but the concepts are exactly the same, just a slightly different syntax.
Hope this helps.
@AdamP
I'd argue there's no harm in sending a message from a VM to a page with a map (like my sample) when the map doesn't support binding out of the box. These are edge cases though.
@NMackay - well I suppose we can make an exception if no binding relationship is possible
@NMackay , Thanks for the MVVM Light Messenger implementation. I will check with MVVM and update.
@AdamP , In Page1 button command event we have subscribed the Messaging Center .
Say for Example with login page.
1. login validation is done in view model and if invalid user information provided then will subscribe the messaging Center to View for showing the DisplayAlert.
2. if valid informations provided we will navigate to page2 (menu screen) .
3. From Page2 to Page1 , we used the PopAsync for coming back to page1.
Hi @adamp, Kindly suggest on this.
What are you trying to do, not in technical terms but explain it to me like I'm a two year old?
Where is the code you are using to subscribe to the message and where are you sending the message?
@GeraldVersluis ,
We will show the DisplayAlert from ViewModel to View by using MessagingCenter in Login Screen with Validations.
After Successful login, we will navigate to menu screen and Unsubscribe the Login MessagingCenter Items in Login Page OnDisappearing Event.
Once user Press the Logout button in Menu Screen, we will use "Navigation.PopToRootAsync()" to navigate (top of the application page) Login Screen.
But, After navigating again to Login screen, the same MessagingCenter is not Subscribing to the Validations in Login Screen.