Windows store app roamingsettings测试是否存在值

时间:2013-02-18 08:52:22

标签: windows-8 windows-store-apps

我想知道关键声音的值是否存在,但下面的代码不适用于我

if (roamingSettings.Values["Sound"] == null)

3 个答案:

答案 0 :(得分:2)

"值"是一个集合,其成员可能会这样检查。

if(roamingSettings.Values.ContainsKey("Sound"))
{
    var myRoamingSettingValue = roamingSettings.Values["Sound"];
    // do stuff with the value you pulled back
}
else
{
    // your roaming settings collection doesn't contain the value you are interested in.
    // Add it?
    roamingSettings.Values.Add("Sound", "myDefaultValue");
}

答案 1 :(得分:1)

我想你想要这个

/// <summary>Returns if a setting is found in the specified storage strategy</summary>
/// <param name="key">Path of the setting in storage</param>
/// <param name="location">Location storage strategy</param>
/// <returns>Boolean: true if found, false if not found</returns>
public static bool SettingExists(string key, StorageStrategies location = StorageStrategies.Local)
{
    switch (location)
    {
        case StorageStrategies.Local:
            return Windows.Storage.ApplicationData.Current.LocalSettings.Values.ContainsKey(key);
        case StorageStrategies.Roaming:
            return Windows.Storage.ApplicationData.Current.RoamingSettings.Values.ContainsKey(key);
        default:
            throw new NotSupportedException(location.ToString());
    }
}
public enum StorageStrategies
{
    /// <summary>Local, isolated folder</summary>
    Local,
    /// <summary>Cloud, isolated folder. 100k cumulative limit.</summary>
    Roaming,
    /// <summary>Local, temporary folder (not for settings)</summary>
    Temporary
}

你会这样称呼:

var _Exists = SettingExists("Sound", StorageStrategies.Roaming);

这取自我的StorageHelper:http://codepaste.net/gtu5mq

答案 2 :(得分:0)

也许您可以尝试编写这样的代码:

    if(string.IsNullOrWhiteSpace(roamingSettings.Values["Sound"]))