Created
November 15, 2011 00:57
-
-
Save JoshClose/1365757 to your computer and use it in GitHub Desktop.
Navigation with MVVM on WP7
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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