[英]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.