2016-10-14 102 views
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方法見我試圖使它在給方法,但沒有一個運作。每次我按回應按鈕應用程序崩潰沒有任何錯誤。

+0

看看http://stackoverflow.com/questions/24335925/Windows的手機-8-1-通用-APP-終止-ON-導航回從 - 第2張 – Bugs

回答

0

我不認爲該應用程序崩潰,它只是退出,因爲這是後退按鈕的默認行爲。

你需要做的是什麼,你已經加入這行代碼在你BackPressed事件處理程序處理的後退按鈕標誌:

e.Handled = true; 
相關問題