如何在C#中设置“First-Launch-View”

时间:2016-02-03 11:37:45

标签: c# windows xaml uwp

我到处搜索,但我找不到我的问题的教程。我想在第一次启动应用程序时设置要显示的页面。类似于:

首次发布: Greeting.xaml> Setting.xaml> MainPage.xaml

定期启动直接进入MainPage。

我该怎么做?

我不是指Splashscreen,我的意思是一个页面,只在你第一次启动App时显示,就像一个小教程。

3 个答案:

答案 0 :(得分:5)

典型的模板生成的App.xaml.csOnLaunched方法中有类似的内容:

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

这是您导航到第一页的位置。对于第一次运行的特殊情况,请执行以下操作:

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

然后,问候页面应该导航到您的设置页面,该页面应该导航到您的主页面。

通过使用漫游设置,用户登录到其他计算机时不会看到第一次显示的屏幕。

答案 1 :(得分:3)

您可以设置"首先" App.xaml.cs中的页面搜索OnLaunched void并将rootFrame.Navigate(typeof(MainPage));更改为rootFrame.Navigate(typeof(Greeting));或您想要调用它。

下一步是检查应用是否首次启动。您可以设置应用程序设置来执行此操作。  1.为Greeting.xaml创建OnnavigatedTo void(只需键入" protected override void onna&#34 ;, IntelliSense将向您建议)并通过插入" async"来使其异步。在" protected",2。使用此代码:

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

我还没有对代码进行过测试,但它应该可行。如果它没有,请问我。

编辑:这是一个更好的解决方案:D https://stackoverflow.com/a/35176403/3146261

答案 2 :(得分:0)

我只是希望通过MessageBox接受免责声明

            IPropertySet roamingProperties = ApplicationData.Current.RoamingSettings.Values;
            if (!roamingProperties.ContainsKey("DisclaimerAccepted"))
            {
                var dialog = new MessageDialog(strings.Disclaimer);
                dialog.Title = "Disclaimer";
                dialog.Commands.Clear();
                dialog.Commands.Add(new UICommand { Label = "Accept", Id = 0 });

                dialog.Commands.Add(new UICommand { Label = "Decline", Id = 1 });
                var result = await dialog.ShowAsync();
                if ((int)result.Id == 1)
                    Application.Current.Exit();
                roamingProperties["DisclaimerAccepted"] = bool.TrueString; 
            }

我把它放在App.xaml.cs里面:

if (e.PrelaunchActivated == false)
        {
            <Inside here>
            if (rootFrame.Content == null)
            {
            }