2014-12-22 235 views
0

我試圖檢查用戶是否在允許導航到任何頁面之前登錄,如果沒有,它將重定向到登錄頁面。我在一個框架元素中工作。WPF。導航堆棧溢出

如果用戶沒有登錄,當前的代碼會創建一個無限循環。我可以調用它,還可以傳遞事件處理程序的代碼,告訴它忽略該代碼行。

public MainWindow() { 
      InitializeComponent(); 
      FrameWindow.Navigating+=FrameWindow_Navigating; 
     } 

     private void FrameWindow_Navigating(object sender, NavigatingCancelEventArgs e) { 
      try { 
       if (UserLoggedIn()) { 
        //continue 
       } 
       else { 
        Uri uri = new Uri("Login.xaml", UriKind.Relative); 
        FrameWindow.NavigationService.Navigate(uri); 
       } 
      } 
      catch (Exception ex){ 
       throw ex; 
      } 
     } 

回答

0

修正這樣做...不知道這是否違反任何約定或不?

private void FrameWindow_Navigating(object sender, NavigatingCancelEventArgs e) { 
      try { 
       bool alreadyHeadingToHome = false; 

       if (e.ExtraData != null){ 
        alreadyHeadingToHome = (bool)e.ExtraData; 
       } 

       if (UserLoggedIn() || alreadyHeadingToHome) { 
       } 
       else { 
        Uri uri = new Uri("Login.xaml", UriKind.Relative); 
        FrameWindow.NavigationService.Navigate(uri, true); 
       } 
      } 
     catch (Exception ex){ 
      throw ex; 
     } 
    }