2016-09-21 61 views
0

簡而言之,我只想在特定頁面上處理返回按鈕以返回到上一頁,但在其餘頁面上忽略(讓默認操作/關閉應用程序)。如何處理僅在特定頁面上的後退按鈕?

所以對於我的例子,我想只處理返回按鈕CurrentPage。所以,我把我的CurrentPage.xaml.cs

public CurrentPage() 
{ 
    this.InitializeComponent(); 
    SystemNavigationManager.GetForCurrentView().BackRequested += App_BackRequested; 
} 

private void App_BackRequested(object sender, BackRequestedEventArgs e) 
{ 
    this.Frame.Navigate(typeof(PreviousPage), e); 
    e.Handled = true; 
} 

這個代碼,但是,當我坐進|上一頁,按後退按鈕將再次去|上一頁。我無法退出該應用程序。

如果我將其添加到PreviousPage.xaml.cs中,則從CurrentPage中按BackButton將導致返回到PreviousPage並自動關閉該應用程序。

public PreviousPage() 
{ 
    this.InitializeComponent(); 
    SystemNavigationManager.GetForCurrentView().BackRequested += App_BackRequested; 
} 

private void App_BackRequested(object sender, BackRequestedEventArgs e) 
{ 
    Application.Current.Exit(); 
    e.Handled = true; 
} 

你有什麼想法如何處理單個頁面上的後退按鈕嗎?

回答

0

當你離開當前頁面時,你還應該刪除後退按下的事件處理程序SystemNavigationManager.GetForCurrentView().BackRequested -= App_BackRequested;。沒有這個代碼就會被調用,即使你已經離開了頁面。

+0

我沒有注意到,謝謝,它解決了我的問題。 – kurakura88