localSettings.Containers [containername] - 给定的密钥不存在

时间:2016-01-16 02:26:58

标签: c# crash

我正在遍历寻找localsettings的代码。当容器为空时,它会给出错误消息"给定的密钥不存在于字典中#34;。如何检查容器是否为null,这样它不会导致代码崩溃?

if ((Windows.Storage.ApplicationDataContainer)localSettings.Containers[containername] != null)

这会产生同样的错误

var container = localSettings.Containers[containername];

1 个答案:

答案 0 :(得分:0)

Windows.Storage.ApplicationDataContainer settings = Windows.Storage.ApplicationData.Current.LocalSettings;

        //This checks if the given container name exists or not
        if(settings.Containers.ContainsKey("containerName"))
        { 
            if(settings.Containers["ContainerName"].Values.ContainsKey("your data key"))
            {
                //Do something
            }
        }

希望有所帮助