2016-10-13 1 views
1

다음 async OnNavigating 처리기에서 e.Cancel을 false로 복원하면 사용자가 현재 페이지에서 벗어날 수 있어야합니다. 어떻게 든 탐색이 예기치 않게 실패했습니다!WFP MahApps 응용 프로그램에서 "e.Cancel"을 false로 복원하면 작동하지 않습니다.

private async void NavigationService_OnNavigating(object sender, NavigatingCancelEventArgs e) 
{ 
     var model = DataContext as ViewModel; 
     if (model == null || !model.IsDirty) return; 

     e.Cancel = true; 
     var option = MessageDialogResult.Negative; 
     try 
     { 
      var metroWindow = (MainWindow)Application.Current.MainWindow; 
      option = await metroWindow.ShowMessageAsync("Form", "Quit?", MessageDialogStyle.AffirmativeAndNegative); 
     } 
     finally 
     { 
      if (option == MessageDialogResult.Affirmative) 
      //******Allow the user to move away******** 
      e.Cancel = false; 
     } 
} 

아무도 문제를 밝힐 수 있습니까? .Net 스레딩과 관련이있는 것은 무엇입니까?

업데이트 : 문제는 inelegantly 결정 변수 _canNavigate로 해결되었습니다.

private bool _canNavigate = false; 
private async void NavigationService_OnNavigating(object sender, NavigatingCancelEventArgs e) 
{ 
    var model = DataContext as ViewModel; 
    if (model == null || !model.IsDirty) return; 

    if (!_canNavigate) 
    { 
     e.Cancel = true; 
     var metroWindow = (MainWindow)Application.Current.MainWindow; 
     var option = await metroWindow.ShowMessageAsync("Form", "Quit?", MessageDialogStyle.AffirmativeAndNegative); 
     if (option == MessageDialogResult.Affirmative) 
     { 
      _canNavigate = true; 
      _navigationService.Navigate(e.Uri); 
     } 
     else _canNavigate = false; 
    } 
    else 
    { 
     _canNavigate = false; 
    } 
} 
+0

취소 속성의 작동 방식을 알지 못하지만 왜 먼저이 항목을 true로 설정 한 다음 대화 상자를 기반으로 변경합니까? e.Cancel = true를 사용하여 if (option == ...) 문에 else를 추가하는 것이 더 합리적이지 않습니까? – FishySwede

+0

e.Cancel = true;를 주석 처리하면 작동합니까? If 여전히 작동하지 않는다면 네비게이션을 취소하는 또 다른 EventHandler가있을 것입니다. – haindl

+0

@NathanL이 질문을 버렸습니까? 해결 했습니까? – FishySwede

답변

1

결정 변수 _canNavigate으로 문제가 해결되지 않았습니다.

private bool _canNavigate = false; 
private async void NavigationService_OnNavigating(object sender, NavigatingCancelEventArgs e) 
{ 
    var model = DataContext as ViewModel; 
    if (model == null || !model.IsDirty) return; 

    if (!_canNavigate) 
    { 
     e.Cancel = true; 
     var metroWindow = (MainWindow)Application.Current.MainWindow; 
     var option = await metroWindow.ShowMessageAsync("Form", "Quit?", MessageDialogStyle.AffirmativeAndNegative); 
     if (option == MessageDialogResult.Affirmative) 
     { 
      _canNavigate = true; 
      _navigationService.Navigate(e.Uri); 
     } 
     else _canNavigate = false; 
    } 
    else 
    { 
     _canNavigate = false; 
    } 
} 
관련 문제