【问题标题】:How to set a "First-Launch-View" in C#如何在 C# 中设置“首次启动视图”
【发布时间】:2016-02-03 11:37:45
【问题描述】:

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

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

常规启动直接进入 MainPage。

我该怎么做?

我指的不是启动画面,我指的是一个页面,它仅在您第一次启动应用程序时显示,类似于一个小教程。

【问题讨论】:

  • 是的,欢迎使用应用程序新用户...方便提示等

标签: c# windows xaml uwp


【解决方案1】:

您的典型模板生成的App.xaml.cs 在其OnLaunched 方法中有类似的内容:

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

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

通过使用漫游设置,用户在登录到另一台机器时不会看到第一次屏幕。

【讨论】:

    【解决方案2】:

    您可以在 App.xaml.cs 中设置“第一”页。搜索 OnLaunched void 并将 rootFrame.Navigate(typeof(MainPage)); 更改为 rootFrame.Navigate(typeof(Greeting)); 或您喜欢的任何名称。

    下一步是检查应用是否首次启动。您可以设置应用程序设置来执行此操作。 1. 为您的 Greeting.xaml 创建 OnnavigatedTo void(只需输入“protected override void onna”,IntelliSense 会向您建议)并通过在“protected”之后插入“async”来实现异步,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

    【讨论】:

    • 是的,这应该可以。但请注意,Page.OnNavigatedTo 是在 UI 线程上调用的,因此不需要调度程序。
    • 谢谢!我在家的时候试试。 :)
    【解决方案3】:

    我只是希望通过 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)
                {
                }
    

    【讨论】:

      猜你喜欢
      • 2014-07-09
      • 2021-09-04
      • 2015-11-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多