gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 21:39:21 25 4
gpt4 key购买 nike

我的情况与 this guys question 非常相似因为我有一个登录页面,它是我的 MainPage.xaml 文件,但我有另一个名为 SetPassword.xaml 的页面,如果用户尚未设置密码,我想加载它。本质上,这是应用程序在安装后第一次加载。

我在 SO 上花了几个小时尝试各种不同的解决方案(包括我链接到的解决方案),但我只是没有得到任何结果,而且似乎许多解决方案都是针对 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 View 。

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

在我的 App.xaml 文件中,我还尝试了多种不同的方法,同样得到了相同的结果。我可以得到调试消息,但不能得到我正在寻找的导航。我查看了在 Application_Launching over here 上实现一个方法以及在 RootFrame.Navigating+= RootFrameOnNavigating; over here 上实现条件导航但显然我遗漏了一些东西。

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

最佳答案

解决方案很简单。要进行导航,我可以根据我的问题在 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();
}

关于c# - Windows Phone 8.1 检查密码是否设置否则加载新页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23200692/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com