繁体   English   中英

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

[英]Windows Phone 8.1 check if password set else load new page

我对这个问题的情况非常相似,因为我有一个登录页面,这是我的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");
}

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

我也在OnNavigatedTo尝试了这种方法,结果完全相同。

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

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

解决方案很简单。 要进行导航,我可以根据我的问题在App或MainPage中完成导航,但导航不起作用的原因是因为我试图导航到SetPassword.xaml,这是一个<ContentDialog>而不是<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();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM