【问题标题】:An exception of type 'System.Collections.Generic.KeyNotFoundException' occurred in mscorlib.ni.dll but was not handled in user codemscorlib.ni.dll 中出现“System.Collections.Generic.KeyNotFoundException”类型的异常,但未在用户代码中处理
【发布时间】:2015-05-21 16:41:51
【问题描述】:

if (NavigationContext.QueryString["id"] != null)

类型异常

System.Collections.Generic.KeyNotFoundException

发生在mscorlib.ni.dll,但未在用户代码中处理

请帮帮我:(

【问题讨论】:

  • 查询字符串没有“id”键。使用ContainsKey 对其进行测试。解决此问题所需的一切都包含在异常消息中。在寻求帮助之前尝试阅读它。

标签: c#-4.0 visual-studio-2012 windows-phone-8


【解决方案1】:

试试这个-

if(NavigationContext.QueryString.ContainsKey("id")) //this checks if "QueryString" has the key "id"
{
    if (NavigationContext.QueryString["id"] != null) //this checks if the key "id" in "QueryString" is null or not 
    {
        //Do stuff here
    }
}

另外,如果你正在检查一个字符串,最好使用

string id = NavigationContext.QueryString["id"];
if(string.IsNullOrWhiteSpace(id))
{
    //Do Stuff here
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-31
    • 2016-02-13
    • 2018-10-25
    • 2016-09-21
    • 2015-05-30
    • 1970-01-01
    相关资源
    最近更新 更多