0
我正在使用MVVM開發Windows Phone 8.1應用程序。當通過後退按鈕導航頁面時,應用程序崩潰
我有一個包含導航服務如下基本視圖模型類:
public abstract class BaseViewModel : INotifyPropertyChanged
{
protected readonly INavigationService NavigationService;
//....
}
有我的導航服務類:
public class NavigationService : INavigationService
{
public void Navigate(Type destinationPage)
{
((Frame)Window.Current.Content).Navigate(destinationPage);
}
public void Navigate(Type desitnationPage, object parameter)
{
((Frame)Window.Current.Content).Navigate(desitnationPage, parameter);
}
public void GoBack()
{
((Frame)Window.Current.Content).GoBack();
}
}
一切工作正常,當我從XAML綁定命令。當我想覆蓋BackButton
時出現問題。我還創建了基本頁面模型,其中也包含NavigationService
。每一頁都有如下的overridde PF BackPressed
:
public class BasePage : Page
{
protected INavigationService NavigationService => ComponentManager.GetInstance<INavigationService>();
public BasePage()
{
//...
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
HardwareButtons.BackPressed += HardwareButtons_BackPressed;
}
protected virtual void HardwareButtons_BackPressed(object sender, BackPressedEventArgs e)
{
//Frame.Navigate(typeof(MainPage));
(this.DataContext as BaseViewModel)?.Back.Execute(sender);
}
}
正如你在HardwareButtons_BackPressed
方法見我試圖使它在給方法,但沒有一個運作。每次我按回應按鈕應用程序崩潰沒有任何錯誤。
看看http://stackoverflow.com/questions/24335925/Windows的手機-8-1-通用-APP-終止-ON-導航回從 - 第2張 – Bugs