【问题标题】:Windows Phone 8.1 check if password set else load new pageWindows Phone 8.1 检查是否设置了密码,否则加载新页面
【发布时间】:2014-04-21 15:45:11
【问题描述】:

我的情况与this guys question 非常相似,因为我有一个登录页面,它是我的 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 over here 上实现一个方法以及在 RootFrame.Navigating+= RootFrameOnNavigating; over here 上实现条件导航,但显然我遗漏了一些东西。

希望你们更聪明的人可以帮助我让我的导航基于条件值工作?

【问题讨论】:

  • 好吧,您的起始页的构造函数不是调用导航的地方。检查这个,它可能会有所帮助。 blogs.msdn.com/b/ptorr/archive/2010/08/28/… 另外,您对 RootFrameOnNavigating 进行了哪些尝试,但没有成功?显示一些代码,更好地描述您的问题。不要只发布其他问题的链接。
  • 我很尴尬,我想通了,现在将发布解决方案。

标签: c# windows-runtime windows-phone windows-phone-8.1 win-universal-app


【解决方案1】:

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

【讨论】:

  • 仅供参考,代码中的这一行已过时,因为它从未以任何方式实际使用过,可以删除:Windows.Storage.StorageFolder localFolder = Windows.Storage.ApplicationData.Current.LocalFolder; 我认为与其编辑上面的帖子,不如添加一个评论。
  • 这个解决方案按预期工作,但我想知道是否有更干净的 MVVM 方法来解决这个问题。可能是一个中间页面,其中视图模型负责条件导航。
猜你喜欢
  • 2014-11-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-01
  • 2011-04-23
相关资源
最近更新 更多