JSON(WP)中的FirstRun信息

时间:2014-11-01 19:31:35

标签: json windows-phone-7 windows-phone-8

我对Windows Phone中的JSON信息有疑问。 我想显示应用程序是否第一次运行,如果没有,则不显示任何内容。

这是我在JSON上显示信息的功能:

async void NavigationService_Navigated(object sender, NavigationEventArgs e)
    {
        if (e.IsNavigationInitiator
            || !e.IsNavigationInitiator && e.NavigationMode != NavigationMode.Back)
        {
            var navigationInfo = new
            {
                Mode = e.NavigationMode.ToString(),
                From = this.BackStack.Any() ? this.BackStack.Last().Source.ToString() : string.Empty,
                Current = e.Uri.ToString(), 
            };

            var jsonData = Newtonsoft.Json.JsonConvert.SerializeObject(navigationInfo);

            await this.currentApplication.Client.PageView(jsonData);
        }
    }

我想再添加一个模式,From和Current。我想添加IsFirstRun,如果它是我第一次打开应用程序,则为真。

我已经看到这个用于firstRun函数,但我不知道如何将它放在我的代码中。

public static bool IsFirstRun()
{
    if (!settings.Contains(FIRST_RUN_FLAG)) //First time running
    {
        settings.Add(FIRST_RUN_FLAG, false);
        return true;
    }
    return false;   
}

我需要帮助......谢谢!

1 个答案:

答案 0 :(得分:0)

如果你想为First Run创建一个标志,这很简单,

在App.xaml.cs

寻找名为

的函数
// Code to execute when the application is launching (eg, from Start)
// This code will not execute when the application is reactivated
private void Application_Launching(object sender, LaunchingEventArgs e)
{
}

我们想要做的是在此函数中创建一个标志,并且只有在它不存在时才将其设置为true。像这样。

using System.IO.IsolatedStorage;  // include this namespace in App.xaml.cs

// Code to execute when the application is launching (eg, from Start)
// This code will not execute when the application is reactivated
private void Application_Launching(object sender, LaunchingEventArgs e)
{
    if (!IsolatedStorageSettings.ApplicationSettings.Contains("first_run"))
    {
        IsolatedStorageSettings.ApplicationSettings.Add("first_run", true);
    }
    else
    {
        // set the flag to flase
        IsolatedStorageSettings.ApplicationSettings["first_run"] = false;
    }

    // save
    IsolatedStorageSettings.ApplicationSettings.Save();
}

现在,如果您想在首次运行时执行某些操作,您只需再次检查设置:

bool first_run = (bool) IsolatedStorageSettings.ApplicationSettings["first_run"];

对于调试案例,您可能希望删除该标志,以便通过执行此操作再次点击first_run

// remove the flag and save
IsolatedStorageSettings.ApplicationSettings.Remove("first_run");
IsolatedStorageSettings.ApplicationSettings.Save();