1

I am wondering how to signal whether an appication is launched for the very first time, or has already been launched before. The reason I want to do this is to show a very short informative message before the application is ever used, while every other time the application is launched nothing shows. Would I place something in App.xaml.cs like the following

var settings = IsolatedStorageSettings.ApplicationSettings;
if (!settings.Contains("WasLaunched")) 
{
  MessageBox.Show("First time to launch");
  settings.Add("WasLaunched", true);
}

And if (!settings.Contains("WasLaunched") navigate to the 'first launch page' as opposed to the 'main page'? Can someone point me to any good references on this implementation?

EDIT**

I changed my WMAppManifest.xml default page to LaunchPage.xaml

<DefaultTask Name="_default" NavigationPage="LaunchPage.xaml" />

And created my UriMapper class

public class LoginUriMapper : UriMapperBase
{
    public override Uri MapUri(Uri uri)
    {
        if (uri.OriginalString == "/LaunchPage.xaml")
        {
            if (Settings.FirstLoad.Value == true)
            {
                //Navigate to Welcome Page with quick first time user info
                uri = new Uri("/Views/WelcomePage.xaml", UriKind.Relative);
            }
            else
            {
                ///Navigate to the actual Main Page
                uri = new Uri("/MainPage.xaml", UriKind.Relative);
            }
        }
        return uri;
    }
}

But how do I change App.xaml.cs accordingly

private void Application_Launching(object sender, LaunchingEventArgs e)
{
    //how to check and navigate to correct page for this specific method?
}

private void Application_Activated(object sender, ActivatedEventArgs e)
{
    //how to check and navigate to correct page for this specific method?
}
Matthew
  • 3,976
  • 15
  • 66
  • 130

2 Answers2

7

You'd better to use the power of UriMapper

Here you can find a good article.

The core idea is:

You should define an empty page (EntryPage.xaml) and set it as a default page of your app. Then in your custom UriMapper you overload the MapUri method.

   public class YourUriMapper : UriMapperBase
   {
    public override Uri MapUri(Uri uri)
    {
        if (uri.OriginalString == "/EntryPage.xaml")
        {
            var settings = IsolatedStorageSettings.ApplicationSettings;

            if (!settings.Contains("WasLaunched"))
            {
                 uri = new Uri("/FirstRunInfoPage.xaml", UriKind.Relative);
            }
            else
            {
                 uri = new Uri("/MainPage.xaml", UriKind.Relative);
             }
         }
            return uri;
     } 
  }

Then on app initialization you should define which UriMapper to use:

private void Application_Launching(object sender, LaunchingEventArgs e)
{
    RootFrame.UriMapper = new YourUriMapper();
}

private void Application_Activated(object sender, ActivatedEventArgs e)
{
    if (e.IsApplicationInstancePreserved == false)
    {
      // tombstoned! Need to restore state
      RootFrame.UriMapper = new YourUriMapper();
    }
}
Courtney Christensen
  • 9,165
  • 5
  • 47
  • 56
Anton Sizikov
  • 9,105
  • 1
  • 28
  • 39
  • Thanks I followed your advice and edited my initial question above. I am just now unsure in `App.xaml.cs` of how to determine which page to navigate to based on whether the application is loading for its first time or regularly? – Matthew Oct 08 '13 at 01:19
  • Worked great! I've noticed though that after hitting the WelcomePage and then navigating to the actual MainPage, the WelcomePage still exists in the back stack. Would I just check this on the OnNavigatedTo event in MainPage and then remove the page from the back stack accordingly? What do you suggest as the best method in this case? – Matthew Oct 08 '13 at 11:43
  • 1
    http://stackoverflow.com/questions/8241529/clearing-backstack-in-navigationservice – Anton Sizikov Oct 08 '13 at 12:12
3

The best approach for the check is to write the status in the Isolated storage as you currently are. To redirect to the appropriate page, I would personally use a URI mapper. that way, your first intended entry page would be in the navigation first entry stack, preventing users from navigating back to the first page. A typical use case would be to redirect a user to a an authentication page when a user isn't authenticated and to a home page when the user is already authenticated, see This example

  public App()
    {
        SetUpLandingPageView();
    }


     void SetUpLandingPageView()
    {
        var isLaunched = IsolatedStorageSettings.ApplicationSettings.Contains("WasLaunched");

        // Get the UriMapper from the app.xaml resources, and assign it to the root frame
        var mapper = Resources["mapper"] as UriMapper;

        if (mapper == null) 
            throw new ArgumentNullException("Mapper must be configured");

        RootFrame.UriMapper = Resources["mapper"] as UriMapper;

        // Update the mapper as appropriate
        mapper.UriMappings[0].MappedUri = isLaunched ? new Uri("/Views/HomePage.xaml", UriKind.Relative) : new Uri("/Views/Introduction.xaml", UriKind.Relative);    
    }

In the app.xaml

Namespace:

xmlns:UriMapper="clr-namespace:System.Windows.Navigation;assembly=Microsoft.Phone"

Xaml

 <Application.Resources>
    <ResourceDictionary>
        <UriMapper:UriMapper x:Name="mapper">
            <UriMapper:UriMapping Uri="/MainPage.xaml" />
        </UriMapper:UriMapper>
    </ResourceDictionary>
</Application.Resources>
FunksMaName
  • 2,101
  • 1
  • 15
  • 17