Domanda

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.

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.

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

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.

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

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?

È stato utile?

Soluzione

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

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!

EDIT:

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();
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top