2011-01-05 12 views
5

Me gustaría saber si hay una bandera de "primera carrera" o similar en WP7. Mi aplicación toma algunas cosas del almacenamiento aislado, por lo que me gustaría determinar si esto es necesario la primera vez. Actualmente estoy usando un if para verificar si el objeto de almacenamiento nombrado existe, pero esto significa que no puedo manejar ningún error de pérdida de memoria de la manera que me gustaría.¿Hay una bandera de "primera ejecución" en WP7

Respuesta

6

No creo que haya una función incorporada para esto ... pero sé lo que quiere decir :-) Implementé "first run" usando iso storage en el código abierto khan academy for windows phone app. Todo lo que hago es buscar en el almacenamiento iso un archivo muy pequeño (solo escribo un byte) ... si no está allí, es la primera vez, si está allí, la aplicación se ejecutó más de una vez. Siéntase libre de visitar la fuente y llevar a mi aplicación si desea :-)

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

¡Eso es genial! ¡Bonito y fácil! – deanvmc

+0

inteligente. podría escribir una versión # en su lugar, para poder detectar actualizaciones, o para aplicaciones con soporte de prueba, también podría usarla para detectar una actualización de prueba a pago. –

+0

Hola Joel, ¿también consideraste no escribir el byte? Preguntándose si optó por esto basado en cualquier observación durante la prueba. En mis propias pruebas breves en esta área, encontré que FileExists funciona bien sin eso. –

4

Como @HenryC sugirió en un comentario sobre la respuesta aceptada he utilizado IsolatedStorageSettings para poner en práctica el "comportamiento primera ejecución", aquí el código:

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

veces tenemos que realizar alguna acción en cada actualización de la tienda de Windows si hay un cambio de versión. Coloque este código en su 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); 

     } 

    } 
Cuestiones relacionadas