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

jus*_*man 6 c# windows-phone windows-runtime windows-phone-8.1 win-universal-app

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

我花了好几个小时尝试各种不同的解决方案(包括我链接的那个),但我只是没有得到任何地方,似乎很多解决方案都是针对WP7或WP8而且没有类似的解决方案WP8.1.

这是使用Windows.Storage进行的基本检查,我正在查看是否已设置密码.

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");
}
Run Code Online (Sandbox Code Playgroud)

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

我也尝试过这种方法,OnNavigatedTo结果完全相同.

在我的App.xaml文件中,我也尝试了许多不同的方法,同样的结果.我可以得到调试消息但不是我正在寻找的导航.我看着实现方法Application_Launching 在这里以及对实施条件导航RootFrame.Navigating+= RootFrameOnNavigating; 在这里,但显然我失去了一些东西.

希望你聪明的人可以帮助我根据条件值让我的导航工作?

jus*_*man 5

解决方案很简单.要做导航,我可以根据我的问题在App或MainPage中完成它,但导航不起作用的原因是因为我试图导航到SetPassword.xaml而<ContentDialog>不是a <Page>.

实际上我觉得我甚至没有检查,但我希望如果这发生在其他人身上,他们可以检查他们是否真的试图导航到一个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();
}
Run Code Online (Sandbox Code Playgroud)