Windows 8 SDK(C#)Application_Deactivated和Application_Closing

时间:2013-10-24 19:12:38

标签: c# windows-8 windows-phone-8

我有一个设置类。此类从IsolatedStorages

获取和设置值
    public Settings() 
    { 
        try 
        { 
            // Get the settings for this application. 
            settings = IsolatedStorageSettings.ApplicationSettings; 

        } 
        catch (Exception e) 
        { 
            Debug.WriteLine("Exception while using IsolatedStorageSettings: " + e.ToString()); 
        } 
    } 


    public int CookieCountSetting 
    { 
        get 
        { 
            return GetValueOrDefault<int>(CookieCountSettingKeyName, CookieCountSettingDefault); 
        } 
        set 
        { 
            if (AddOrUpdateValue(CookieCountSettingKeyName, value)) 
            { 
                Save(); 
            } 
        } 
    }
    public bool AddOrUpdateValue(string Key, Object value) 
    { 
        bool valueChanged = false; 

        // If the key exists 
        if (settings.Contains(Key)) 
        { 
            // If the value has changed 
            if (settings[Key] != value) 
            { 
                // Store the new value 
                settings[Key] = value; 
                valueChanged = true; 
            } 
        } 
        // Otherwise create the key. 
        else 
        { 
            settings.Add(Key, value); 
            valueChanged = true; 
        } 

        return valueChanged; 
    } 



    public valueType GetValueOrDefault<valueType>(string Key, valueType defaultValue) 
    { 
        valueType value; 

        // If the key exists, retrieve the value. 
        if (settings.Contains(Key)) 
        { 
            value = (valueType)settings[Key]; 
        } 
        // Otherwise, use the default value. 
        else 
        { 
            value = defaultValue; 
        } 

        return value; 
    } 

//我也有保存功能和其他一些不相关的代码

现在在我的代码中我使用这个类,并从存储

设置一个变量
    private Settings settings = new Settings(); 

    public int CookieCount; 

    public Game() 
    { 
        InitializeComponent(); 


        CookieCount = settings.CookieCountSetting;

最终我想在应用程序关闭/退出时存储此值。我使用这个代码

    // Code to execute when the application is deactivated (sent to background) 
    // This code will not execute when the application is closing 
    private void Application_Deactivated(object sender, DeactivatedEventArgs e) 
    { 
        settings.CookieCountSetting = CookieCount; 
    } 

    // Code to execute when the application is closing (eg, user hit Back) 
    // This code will not execute when the application is deactivated 
    private void Application_Closing(object sender, ClosingEventArgs e) 
    { 
        settings.CookieCountSetting = CookieCount; 
    }

我的问题是,当我关闭应用程序并再次启动时,该值将恢复为默认值。

我认为这不是事实。

0 个答案:

没有答案