0

我想知道键声音的值是否存在,但下面的代码对我不起作用

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

3 回答 3

2

“Values”是一个集合,可以像这样检查它的成员。

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");
}
于 2013-02-18T12:59:53.380 回答
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

于 2013-02-18T19:02:04.283 回答
0

也许您可以尝试编写如下代码:

    if(string.IsNullOrWhiteSpace(roamingSettings.Values["Sound"]))
于 2013-02-18T08:57:32.593 回答