2011-01-05 10 views
5

Vorrei sapere se c'è un flag "prima esecuzione" o simile in WP7. La mia app prende alcune cose dall'archiviazione isolata, quindi vorrei determinare se è necessario la prima volta. Attualmente sto usando un se per verificare se l'oggetto di memoria con nome esiste ma questo significa che non posso gestire alcun errore di perdita di memoria nel modo in cui vorrei.C'è un flag "prima esecuzione" in WP7

risposta

6

Non credo che ci sia una funzione integrata per questo ... ma so cosa intendi :-) Ho implementato "prima esecuzione" io stesso usando la memoria iso nell'open source khan academy for windows phone app. Tutto ciò che faccio è cercare in iso storage un file molto piccolo (ne scrivo solo un byte) ... se non c'è, è la prima volta che, se è lì, l'app è stata eseguita più di una volta. Sentitevi liberi di controllare la fonte e prendere la mia realizzazione, se vuoi :-)

private static bool hasSeenIntro; 

    /// <summary>Will return false only the first time a user ever runs this. 
    /// Everytime thereafter, a placeholder file will have been written to disk 
    /// and will trigger a value of true.</summary> 
    public static bool HasUserSeenIntro() 
    { 
     if (hasSeenIntro) return true; 

     using (var store = IsolatedStorageFile.GetUserStoreForApplication()) 
     { 
      if (!store.FileExists(LandingBitFileName)) 
      { 
       // just write a placeholder file one byte long so we know they've landed before 
       using (var stream = store.OpenFile(LandingBitFileName, FileMode.Create)) 
       { 
        stream.Write(new byte[] { 1 }, 0, 1); 
       } 
       return false; 
      } 

      hasSeenIntro = true; 
      return true; 
     } 
    } 
+0

È davvero fantastico! Bello e facile! – deanvmc

+0

intelligente. potresti invece scrivere una versione #, in modo da poter rilevare gli aggiornamenti, o per le app con supporto di prova, potresti anche usarla per rilevare un aggiornamento da prova a pagamento. –

+0

Ciao Joel, hai anche considerato di non scrivere il byte? Mi chiedo se hai optato per questo sulla base di eventuali osservazioni durante il test. Nei miei brevi test in quest'area ho trovato FileExists che funziona bene senza quello. –

4

Come @HenryC suggerito in un commento sulla risposta accettata ho usato IsolatedStorageSettings per implementare il "comportamento Prima esecuzione", qui è il codice:

private static string FIRST_RUN_FLAG = "FIRST_RUN_FLAG"; 
    private static IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings; 

    public bool IsFirstRun() 
    { 
     if (!settings.Contains(FIRST_RUN_FLAG)) 
     { 
      settings.Add(FIRST_RUN_FLAG, false); 
      return true; 
     } 
     else 
     { 
      return false; 
     } 
    } 
1

A volte è necessario eseguire un'azione su ogni aggiornamento dall'archivio di Windows in caso di modifica della versione. Inserisci questo codice nella tua app.xaml.cs

private static string FIRST_RUN_FLAG = "FIRST_RUN_FLAG"; 
    private static IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings; 

    private static string _CurrentVersion; 

    public static string CurrentVersion 
    { 
     get 
     { 
      if (_CurrentVersion == null) 
      { 
       var versionAttribute = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyFileVersionAttribute), true).FirstOrDefault() as AssemblyFileVersionAttribute; 
       if (versionAttribute != null) 
       { 
        _CurrentVersion = versionAttribute.Version; 
       } 
       else _CurrentVersion = ""; 
      } 

      return _CurrentVersion; 

     } 

    } 

    public static void OnFirstUpdate(Action<String> action) 
    { 
     if (!settings.Contains(FIRST_RUN_FLAG)) 
     { 
      settings.Add(FIRST_RUN_FLAG, CurrentVersion); 
      action(CurrentVersion); 
     } 
     else if (((string)settings[FIRST_RUN_FLAG]) != CurrentVersion) //It Exits But Version do not match 
     { 
      settings[FIRST_RUN_FLAG] = CurrentVersion; 
      action(CurrentVersion); 

     } 

    }