Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Navigation with MVVM on WP7
using System.Windows.Navigation;
using Microsoft.Practices.Prism.ViewModel;
namespace RememberIt.WP7.App.ViewModels
{
public abstract class ViewModelBase : NotificationObject
{
protected bool RemoveBackEntry { get; set; }
public NavigationService NavigationService { get; set; }
public NavigationContext NavigationContext { get; set; }
public virtual void OnNavigatedTo( NavigationEventArgs e ) {}
public virtual void OnNavigatingFrom( NavigatingCancelEventArgs e ) {}
public virtual void OnNavigatedFrom( NavigationEventArgs e )
{
if( RemoveBackEntry )
{
RemoveBackEntry = false;
NavigationService.RemoveBackEntry();
}
}
}
}
using Microsoft.Phone.Controls;
using RememberIt.WP7.App.ViewModels;
namespace RememberIt.WP7.App.Views
{
public class PageBase : PhoneApplicationPage
{
protected PageBase()
{
Loaded += PageBaseLoaded;
}
private void PageBaseLoaded( object sender, System.Windows.RoutedEventArgs e )
{
var viewModel = DataContext as ViewModelBase;
if( viewModel != null )
{
viewModel.NavigationService = NavigationService;
}
}
protected override void OnNavigatedTo( System.Windows.Navigation.NavigationEventArgs e )
{
base.OnNavigatedTo( e );
var viewModel = DataContext as ViewModelBase;
if( viewModel != null )
{
viewModel.NavigationContext = NavigationContext;
viewModel.OnNavigatedTo( e );
}
}
protected override void OnNavigatingFrom( System.Windows.Navigation.NavigatingCancelEventArgs e )
{
base.OnNavigatingFrom( e );
var viewModel = DataContext as ViewModelBase;
if( viewModel != null )
{
viewModel.NavigationContext = NavigationContext;
viewModel.OnNavigatingFrom( e );
}
}
protected override void OnNavigatedFrom( System.Windows.Navigation.NavigationEventArgs e )
{
base.OnNavigatedFrom( e );
var viewModel = DataContext as ViewModelBase;
if( viewModel != null )
{
viewModel.NavigationContext = NavigationContext;
viewModel.OnNavigatedFrom( e );
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment