Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set a "First-Launch-View" in C#

Tags:

c#

windows

xaml

uwp

I searched everywhere, but i can't find a tutorial for my problem. I want to set an page to be shown, when the App is launched for the first time. something like th:

First launch: Greeting.xaml>Setting.xaml>MainPage.xaml

Regular launch goes directly to MainPage.

how can i do this?

I didn't mean a Splashscreen, I mean a page, which is shown only the first time you launch the App, something like a little tutorial.

like image 756
Flauschcoder Avatar asked Feb 07 '23 20:02

Flauschcoder


2 Answers

Your typical template-generated App.xaml.cs has something like this in its OnLaunched method:

if (rootFrame.Content == null)
{
    rootFrame.Navigate(typeof(MainPage), e.Arguments);
}

This is where you navigate to your first page. To special-case a first run, do something like this instead:

if (rootFrame.Content == null)
{
    IPropertySet roamingProperties = ApplicationData.Current.RoamingSettings.Values;
    if (roamingProperties.ContainsKey("HasBeenHereBefore"))
    {
        // The normal case
        rootFrame.Navigate(typeof(MainPage), e.Arguments);
    }
    else
    {
        // The first-time case
        rootFrame.Navigate(typeof(GreetingsPage), e.Arguments);
        roamingProperties["HasBeenHereBefore"] = bool.TrueString; // Doesn't really matter what
    }
}

The greetings page should then navigate to your settings page, which should navigate to your main page.

And by using the roaming settings, the user won't see the first-time screen when she logs in to a different machine.

like image 114
Petter Hesselberg Avatar answered Feb 10 '23 10:02

Petter Hesselberg


You can set the "first" page within the App.xaml.cs. Search for the OnLaunched void and change rootFrame.Navigate(typeof(MainPage)); to rootFrame.Navigate(typeof(Greeting)); or whtatever you like to call it.

The next step would be to check if the app launches for the first time. You can set an app setting to do that. 1. create the OnnavigatedTo void for your Greeting.xaml (just type "protected override void onna", IntelliSense will suggest it to you) and make is asynchron by inserting "async" after "protected", 2. use this code:

if (ApplicationData.Current.LocalSettings.Values.ContainsKey("isFirstLaunch"))
{
    // if that's the first launch, stay, otherwise navigate to Settings.xaml
    if (!(bool)ApplicationData.Current.LocalSettings.Values["isFirstLaunch"])
    {
        await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => Frame.Navigate(typeof(Settings)));
    }
}
else
{
    ApplicationData.Current.LocalSettings.Values["isFirstLaunch"] = false;
}

I haven't tested the code but it should work. If it doesn't, just ask me.

Edit: here's a much better solution :D https://stackoverflow.com/a/35176403/3146261

like image 45
Nic Avatar answered Feb 10 '23 09:02

Nic