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

标签 c# xml xaml windows-phone-7

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

我需要在 Windows Phone 中读取 IsolatedStorage 的两个设置。设置为 ExitAlertOrientationLock。我已经设置好了,它们保存得很好,我只需要知道如何从另一个页面读取它们。设置变量的页面是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
{
}

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

关于c# - 从另一个页面读取 IsolatedStorage 中的设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8114350/

相关文章:

c# - .Net DateTime 中的毫秒格式

c# - Unity3d 在构建后看不到项目文件夹中的 XML 文件

WPF Storyboard动画即使在设置后也永远循环

c# - 动画对象不能用于动画 RenderTransform

c# - 将数据源与文本框一起使用

c# - 用于获取分组表中具有最大日期的行的 LINQ 查询

c# - 为什么不能将 Array.Length 和 Array.Count() 分配给 const 值?

c# - 如何将多个表作为一个 XML 返回?

c - 将大量 xml 文件转换为文本时出现段错误

xaml - 在 ContentView 中声明转换器会导致空白的 xamarin 表单页面