2016-03-25 158 views
0

正如在oficial doc中提到的,有可能處理返回按鈕按下,但我無法處理它只做我的邏輯不回去。所以我的代碼如下所示:覆蓋後退按鈕按uwp

//declare event 
public event EventHandler<BackRequestedEventArgs> BackRequested; 

public MainPage() { 
    InitializeComponent(); 

    // tie my event with back action 
    SystemNavigationManager.GetForCurrentView().BackRequested += OnBackRequested; 
} 

// handle the back event 
private void OnBackRequested(object sender, BackRequestedEventArgs e) { 
    //need to do only my logic here - do NOT go back 
} 

所以有一種方法來獲得這種行爲。我想它由於Android做到這一點:

@Override 
public void onBackPressed() { 
    //does only my logic 
} 

回答

1

你可以爭論參數e.Handled設置爲true,這樣它可以讓你實現自己的邏輯,你可以選擇是否要回去通過堆棧。

private void OnBackRequested(object sender, BackRequestedEventArgs e) 
    { 
     Frame rootFrame = Window.Current.Content as Frame; 

     if (rootFrame.CanGoBack) 
     { 
      e.Handled = true; 

      if(condition) //my condition is true so go back through the stack 
       rootFrame.GoBack(); 
      } 
     } 
    } 
+1

'e.Handled = true;'這就是我所需要的,thx – Choletski