Skip to content

Instantly share code, notes, and snippets.

@JoshClose
Created November 15, 2011 00:57
Show Gist options
  • Save JoshClose/1365757 to your computer and use it in GitHub Desktop.
Save JoshClose/1365757 to your computer and use it in GitHub Desktop.
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