Windows Phone 8.1检查是否设置了密码,否则将加载新页面 [英] Windows Phone 8.1 check if password set else load new page

查看:52
本文介绍了Windows Phone 8.1检查是否设置了密码,否则将加载新页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I Have a very similar situation to this guys question in that I have a Login Page which is my MainPage.xaml file but I have another page called SetPassword.xaml that I want to load if a user has not set a password yet. Essentially this is the first time that the App loads after it has been installed.

我花了数小时在SO上尝试各种不同的解决方案(包括与我链接的解决方案),但是我却一无所获,似乎许多解决方案都是针对WP7或WP8的,没有解决任何类似的问题用于新的WP8.1.

I've spent hours on SO trying various different solutions (including the one I linked to) but I'm just not getting anywhere and it seems that many of the solutions are either for WP7 or WP8 and nothing similar has been solved for the new WP8.1.

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

This is the basic check, using Windows.Storage that I'm doing to see if a password has been set or not.

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视图.

If I add this to public MainPage() class I have no problem in the app returning "Password is not set" in the debug messages however the this.frame.Navigate(typeof(SetPassword)) navigation never loads the SetPassword view.

我也在 OnNavigatedTo 中尝试了此方法,结果完全相同.

I have also tried this method in the OnNavigatedTo with exactly the same results.

在我的App.xaml文件中,我还尝试了许多不同的方法,但结果相同.我可以获取调试消息,但找不到所需的导航.我看过在 Application_Launching 在此处上实现方法,以及在上实现条件导航> RootFrame.Navigating + = RootFrameOnNavigating; 在此处,但显然我遗漏了一些东西.

In my App.xaml file I've also tried a number of different methods, again, with the same results. I can get the debug message but not the navigation I'm looking for. I looked at implementing a method on Application_Launching over here as well as implementing conditional navigation on RootFrame.Navigating+= RootFrameOnNavigating; over here but clearly I am missing something.

希望您更聪明的人可以帮助我根据条件值来完成我的导航工作吗?

Hopefully you smarter people can help me get my navigation working based on a conditional value?

推荐答案

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

The solution was simple. To do the navigation I could have done it in either App or MainPage as per my question but the reason the navigation wasn't working was because I was trying to navigate to SetPassword.xaml which was a <ContentDialog> instead of a <Page>.

实际上,我什至没有检查一下就感到很尴尬,但是希望这是否发生在其他人身上,他们可以检查他们实际上是在尝试导航到Page而不是其他任何类型的元素.我真可笑啊!

I feel embarrassed actually that I didn't even check that but hopefully if this happens to someone else they can check that they're actually trying to navigate to a Page and not any other type of element. How sadly foolish of me!

这是我在App.xaml文件中的OnLaunched外观,现在我可以进行检查并根据设置的值重定向到另一个页面.

Here's what my OnLaunched in the App.xaml file looks like where I can now do my check and redirect to a different page based on the value being set.

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

这篇关于Windows Phone 8.1检查是否设置了密码,否则将加载新页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆