如何在xamarin交叉表中检查应用程序是否首次运行

时间:2018-02-26 10:19:14

标签: xaml xamarin c#-3.0

我想在第一次安装应用程序时显示一些页面,下次打开应用程序时会显示其他页面。

我试过这段代码

 protected override void OnStart()
    {
        if (Application.Current.Properties.ContainsKey("id"))
        {
            MainPage = new NavigationPage(new Page2());
        }
        else
        {
            Application.Current.Properties["id"] = 2;

            MainPage = new NavigationPage(new Page1());
        }          
    }

属性字典中的值仅在应用程序进入休眠状态时存储

3 个答案:

答案 0 :(得分:2)

对于跨平台方法,您可以使用Settings Plugin

然后您可以创建一个布尔属性,例如DidOpenOnce,如果它是false,则显示您的初始欢迎页面或其他。然后,将其设置为true。

答案 1 :(得分:1)

protected void checkApplicationInstallState()
    {
        //retreive
        var prefs = Application.Context.GetSharedPreferences("MyApp", FileCreationMode.Private);
        var somePref = prefs.GetBool("IsApplicationOpenedForOnce", null);
        if (!somePref) {
            // Your Application is opened for the very first time. Now change the value to true as you have now opened the app so next time opening this application should get a true value.
            var prefEditor = prefs.Edit();
            prefEditor.PutBool(true, "IsApplicationOpenedForOnce");
            prefEditor.Commit();
        }

}

答案 2 :(得分:0)

public MainPage()
        {
            InitializeComponent();
            if (Application.Current.Properties.ContainsKey("FirstUse"))
            {
                //Do things when it's NOT the first use...
                loadinit();
            }
            else
            {
                Application.Current.Properties["FirstUse"] = false;
                //Do things when it IS the first use...

            }


        }
        public async void loadinit()
        {
            await Navigation.PushAsync(new LoadPage());
        }