Skip to main content
 首页 » 编程设计

c#之从另一个页面读取 IsolatedStorage 中的设置

2024年02月23日19txw1958

简单的问题,之前有人问过,但没有人给我正确的答案,所以我会非常具体。

我需要在 Windows Phone 中读取 IsolatedStorage 的两个设置。设置为 ExitAlert 和 OrientationLock。我已经设置好了,它们保存得很好,我只需要知道如何从另一个页面读取它们。设置变量的页面是Settings.cs,我需要从MainPage.xaml.cs中读取设置。我还需要知道如何仅在变量为真或假时才做某事。我想我应该使用 if-then-else 语句,但我只是想仔细检查一下。

我的应用是用 C# 编写的。这是我用来存储设置的代码:

using System; 
using System.IO.IsolatedStorage; 
using System.Diagnostics; 
 
namespace Google_ 
{ 
    public class AppSettings 
    { 
 
        IsolatedStorageSettings isolatedStore; 
 
        // isolated storage key names 
        const string ExitWarningKeyName = "ExitWarning"; 
        const string OrientationLockKeyName = "OrientationLock"; 
 
        // default values 
        const bool ExitWarningDefault = true; 
        const bool OrientationLockDefault = false; 
 
        public AppSettings() 
        { 
            try 
            { 
                // Get the settings for this application. 
                isolatedStore = IsolatedStorageSettings.ApplicationSettings; 
 
            } 
            catch (Exception e) 
            { 
                Debug.WriteLine("Exception while using IsolatedStorageSettings: " + e.ToString()); 
            } 
        } 
 
 
        public bool AddOrUpdateValue(string Key, Object value) 
        { 
            bool valueChanged = false; 
 
            // If the key exists 
            if (isolatedStore.Contains(Key)) 
            { 
                // If the value has changed 
                if (isolatedStore[Key] != value) 
                { 
                    // Store the new value 
                    isolatedStore[Key] = value; 
                    valueChanged = true; 
                } 
            } 
            // Otherwise create the key. 
            else 
            { 
                isolatedStore.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 (isolatedStore.Contains(Key)) 
            { 
                value = (valueType)isolatedStore[Key]; 
            } 
            // Otherwise, use the default value. 
            else 
            { 
                value = defaultValue; 
            } 
 
            return value; 
        } 
 
        public void Save() 
        { 
            isolatedStore.Save(); 
        } 
 
        public bool ExitWarning 
        { 
            get 
            { 
                return GetValueOrDefault<bool>(ExitWarningKeyName, ExitWarningDefault); 
            } 
            set 
            { 
                AddOrUpdateValue(ExitWarningKeyName, value); 
                Save(); 
            } 
        } 
 
        public bool OrientationLock 
        { 
            get 
            { 
                return GetValueOrDefault<bool>(ExitWarningKeyName, OrientationLockDefault); 
            } 
            set 
            { 
                AddOrUpdateValue(OrientationLockKeyName, value); 
                Save(); 
            } 
        } 
 
    } 
} 

如果有任何不清楚的地方,请告诉我,以便我进一步解释。谢谢。

请您参考如下方法:

我所做的是将以下内容添加到我的 AppSettings 类中:

private static AppSettings _default = new AppSettings(); 
/// <summary> 
/// Gets the default instance of the settings 
/// </summary> 
public static AppSettings Default 
{ 
    get 
    { 
        return _default;  
    } 
} 

然后您可以在项目的任何位置使用 AppSettings.Default:

if (AppSettings.Default.ExitWarning) 
{ 
} 
else 
{ 
} 

如果您需要更多信息,请告诉我。