2014-11-01 53 views
1

我在Windows Phone中的JSON信息有問題。 我想要顯示該應用是否第一次運行,如果沒有,請不要顯示任何內容。JSON中的FirstRun信息(WP)

這是我的函數來顯示對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); 
     } 
    } 

我想補充一點,其中是模式,從和電流。我想添加IsFirstRun,如果這是我第一次打開該應用程序,則返回True。

我見過firstRun函數,但我不知道如何把它放在我的代碼中。

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

我需要幫助......謝謝!

+0

任何幫助,請再次命中first_run? – Javi 2014-11-01 21:53:48

回答

0

,如果你想創建一個首次運行的標誌,是非常簡單的,

在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"]; 

對於調試情況下,你可能會想刪除標誌,這樣它將通過這樣

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