Skip to content

Instantly share code, notes, and snippets.

@Cheesebaron
Created June 17, 2015 13:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Cheesebaron/3d33daf4b76dc091e26e to your computer and use it in GitHub Desktop.
Save Cheesebaron/3d33daf4b76dc091e26e to your computer and use it in GitHub Desktop.
MvvmCross Windows Phone 8.1 BaseView
using System.Windows.Input;
using Windows.Phone.UI.Input;
using Windows.UI.Xaml.Navigation;
using Cirrious.MvvmCross.ViewModels;
using Cirrious.MvvmCross.WindowsCommon.Views;
namespace Lolz.WindowsPhone.Views
{
public abstract class BaseView<T> : MvxWindowsPage<T> where T : MvxViewModel
{
private ICommand _goBackCommand;
public ICommand GoBackCommand
{
get { return _goBackCommand ?? (_goBackCommand = new MvxCommand(GoBack, CanGoBack)); }
set { _goBackCommand = value; }
}
protected BaseView()
{
NavigationCacheMode = NavigationCacheMode.Required;
Loaded += (s, e) => {
HardwareButtons.BackPressed += HardwareButtonsBackPressed;
};
Unloaded += (s, e) => {
HardwareButtons.BackPressed -= HardwareButtonsBackPressed;
};
}
private void HardwareButtonsBackPressed(object sender, BackPressedEventArgs e)
{
if (GoBackCommand.CanExecute(null))
{
e.Handled = true;
GoBackCommand.Execute(null);
}
}
public virtual void GoBack()
{
if (Frame != null && Frame.CanGoBack) Frame.GoBack();
}
public virtual bool CanGoBack()
{
return Frame != null && Frame.CanGoBack;
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
if (e.NavigationMode == NavigationMode.New)
ViewModel = null;
base.OnNavigatedTo(e);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment