Reputation: 684
Looking for comments on the following scheme for handling page navigation. Using the Mvvm Light Messenger sends messages in a broadcast manner so if all ViewModels in a multi page solution listen to same type of message, every one will receive all messages. Filtering out the ones the current ViewModel needs to handle is done by HandleIncomingMessage()
Also I wonder where to store "globalish" data that flow through the app, have used static properties defined in App.xaml.cs so far for currentCustomerId etc. But should I also put the object graph with all person data from the database here?
An alternative would be to extend or overload the PageTransitionMessageType() and provide properties to send specific messages to each page. In this way you would not have to worry about Filtering of incoming messages described above.
Any comments appreciated!
// In ViewModelLocator
public static readonly Uri Page1Uri = new Uri("/Views/Page1.xaml", UriKind.Relative);
public static readonly Uri Page2Uri = new Uri("/Views/Page2.xaml", UriKind.Relative);
public static readonly Uri Page3Uri = new Uri("/Views/Page3.xaml", UriKind.Relative);
// create similar page def for Page2
public partial class Page1 : PhoneApplicationPage
{
public Page1()
{
InitializeComponent();
Messenger.Default.Register<PageTransitionMessageType>(this, (action) => NavigationHandler(action));
}
private void NavigationHandler(PageTransitionMessageType action)
{
NavigationService.Navigate(action.PageUri);
}
}
// create similar VM for Page3
public class Page2ViewModel : ViewModelBase
{
public Page2ViewModel ()
{
Messenger.Default.Register<PageTransitionMessageType>(this, (s) => HandleIncomingMessage(s));
}
private void HandleIncomingMessage(PageTransitionMessageType s)
{
// check for page2 message
if (s.PageUri == ViewModelLocator.Page2Uri)
{
// do cunning page2 stuff...
}
}
}
// create similar VM for Page2
public class Page1ViewModel : ViewModelBase
{
public RelayCommand GotoPage2Cmd { get; private set; }
public Page1ViewModel()
{
GotoPage2Cmd = new RelayCommand(() => ExecuteGoToPage2(), () => CanExecuteGoToPage2());
}
private void ExecuteGoToPage2()
{
var message = new PageTransitionMessageType() { PageUri = ViewModelLocator.Page2Uri };
Messenger.Default.Send<PageTransitionMessageType>(message);
}
}
public class PageTransitionMessageType
{
public Uri PageUri { get; set; }
// e.g. put props with data you'd like to pass from one page to another here
}
Upvotes: 0
Views: 232
Reputation: 2127
I would recommend saving "globalish" variables in an IsolatedStorage
Upvotes: 1