Windows Phone 8.1检查密码设置是否加载新页面



我有一个与这个问题非常相似的情况,我有一张登录页面,这是我的MainPage.xaml文件,但我有另一个名为SetPassword.xaml的页面,如果用户还没有设置密码,我想加载它。本质上,这是应用程序安装后首次加载。

我花了几个小时在SO上尝试各种不同的解决方案(包括我链接的解决方案),但我没有取得任何进展,似乎许多解决方案都是针对WP7或WP8的,而新的WP8.1没有解决类似的问题。

这是使用Windows进行的基本检查。我正在进行的存储,以查看是否已设置密码。

Windows.Storage.ApplicationDataContainer localSettings = Windows.Storage.ApplicationData.Current.LocalSettings;
Windows.Storage.StorageFolder localFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
if (localSettings.Values["myPassword"] == null)
{
    Debug.WriteLine("Password not set");
    this.Frame.Navigate(typeof(SetPassword));
}
else
{
    Debug.WriteLine("Password is set, continuing as normal");
}

如果我将其添加到public MainPage()类中,我在应用程序中返回调试消息中的"未设置密码"没有问题,但是this.frame.Navigate(typeof(SetPassword))导航从不加载SetPassword视图。

我也在OnNavigatedTo中尝试过这种方法,得到了完全相同的结果。

在我的App.xaml文件中,我也尝试了许多不同的方法,同样得到了相同的结果。我可以得到调试消息,但不能得到我要找的导航。我考虑过在Application_Launching上实现一个方法,以及在RootFrame.Navigating+= RootFrameOnNavigating;上实现条件导航,但显然我遗漏了一些内容。

希望你们这些更聪明的人能帮助我根据条件值进行导航?

解决方案很简单。根据我的问题,我本可以在应用程序或主页中进行导航,但导航不起作用的原因是我试图导航到SetPassword.xaml,它是<ContentDialog>而不是<Page>

事实上,我感到很尴尬,因为我甚至没有检查,但希望如果这种情况发生在其他人身上,他们可以检查他们是否真的试图导航到页面,而不是任何其他类型的元素。我真是愚蠢至极!

编辑:

以下是我在App.xaml文件中的OnLaunched的样子,我现在可以根据设置的值进行检查并重定向到不同的页面。

protected override void OnLaunched(LaunchActivatedEventArgs e)
{
    Frame rootFrame = Window.Current.Content as Frame;
    if (rootFrame == null)
    {
        rootFrame = new Frame();
        rootFrame.CacheSize = 1;
        Window.Current.Content = rootFrame;
        // The following checks to see if the value of the password is set and if it is not it redirects to the save password page - else it loads the main page.
        Windows.Storage.ApplicationDataContainer localSettings = Windows.Storage.ApplicationData.Current.LocalSettings;
        Windows.Storage.StorageFolder localFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
        if (localSettings.Values["myPassword"] == null)
        {
            rootFrame.Navigate(typeof(SetPassword));
        }
        else
        {
            rootFrame.Navigate(typeof(MainPage));
        }
    }
    if (rootFrame.Content == null)
    {
        if (rootFrame.ContentTransitions != null)
        {
            this.transitions = new TransitionCollection();
            foreach (var c in rootFrame.ContentTransitions)
            {
                this.transitions.Add(c);
            }
        }
        rootFrame.ContentTransitions = null;
        rootFrame.Navigated += this.RootFrame_FirstNavigated;
        if (!rootFrame.Navigate(typeof(MainPage), e.Arguments))
        {
            throw new Exception("Failed to create initial page");
        }
    }
    Window.Current.Activate();
}

最新更新