Domanda

Vorrei sapere se v'è una bandiera "prima esecuzione" o simili in WP7. La mia app richiede un po 'roba fuori di stoccaggio isolato quindi vorrei per determinare se ciò è necessario prima volta. Attualmente sto usando un if per verificare se esiste l'oggetto di archiviazione chiamato, ma questo significa che non riesco a gestire eventuali errori di perdita di memoria nel modo in cui mi piacerebbe.

È stato utile?

Soluzione

Non credo che ci sia un costruito in funzione di questo ... ma so cosa vuoi dire :-) ho implementato "prima esecuzione" me stesso usando stoccaggio iso in open source Khan Academy per Windows phone App . Tutto ciò che faccio è guardare in deposito iso per un tempo molto piccolo file (ho appena scrivere un byte ad esso) ... se non è lì, è la prima volta, se è lì, l'applicazione è 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;
        }
    }

Altri suggerimenti

Come @HenryC suggerito in un commento sulla risposta accettata ho usato IsolatedStorageSettings per implementare il "comportamento Prima esecuzione", ecco 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;
        }
    }

A volte abbiamo bisogno di eseguire alcune azioni su ogni aggiornamento dal negozio di Windows se non v'è il cambiamento di versione. Mettete questo codice nel tuo 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);

        }

    }
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top