WPF。导航Stack Overflow

时间:2014-12-22 17:43:14

标签: wpf navigation frame stack-overflow

我试图在允许导航到任何页面之前检查用户是否已登录,否则它将重定向到登录页面。我在框架元素中完成所有这些工作。

如果用户未登录,当前代码会创建一个无限循环。我可以调用它并向事件处理程序传递一个代码,告诉它忽略.navigate这一行吗?

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;
            }
        }

1 个答案:

答案 0 :(得分: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;
        }
    }