Created
June 17, 2015 13:54
-
-
Save Cheesebaron/3d33daf4b76dc091e26e to your computer and use it in GitHub Desktop.
MvvmCross Windows Phone 8.1 BaseView
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.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