C# 如何检查NavigationContext.QueryString?
在ASP.NET中,我可以检查是否存在这样的查询字符串键/值C# 如何检查NavigationContext.QueryString?,c#,windows-phone-8,query-string,C#,Windows Phone 8,Query String,在ASP.NET中,我可以检查是否存在这样的查询字符串键/值 if(Request.QueryString["someValue"] != null) 但是,我无法使用NavigationContext.QueryString if(NavigationContext.QueryString["someValue"] != null) 抛出错误-字典中不存在给定的键 if(NavigationContext.QueryString.ContainsKey("someValue")) 也
if(Request.QueryString["someValue"] != null)
但是,我无法使用NavigationContext.QueryString
if(NavigationContext.QueryString["someValue"] != null)
抛出错误-字典中不存在给定的键
if(NavigationContext.QueryString.ContainsKey("someValue"))
也会抛出一个错误。此代码位于on导航到它应该位于的方法中
如何检查Windows Phone 8中是否存在密钥/值?我目前的难看的解决方法是将这些块中的每一个都包含在一个try/catch中,catch块中没有代码。如果密钥存在,代码将完成,如果不存在,则抛出错误,并以静默方式捕获 if(NavigationContext.QueryString[“someValue”!=null)
应该可以正常工作。你什么时候叫它?您应该在导航到的中执行此操作。如果您在控件中使用它,或者导航上下文
为空。通过检查QueryString是否包含该键,可能您的上下文不正确。否则,请尝试获取值。但是,检查错误是否在提取过程中,而不是通过访问NavigationContext或QueryString(可能是null)来检查
我在导航到时登记。我得到的错误是给定的键在字典中不存在
protected override void OnNavigatedTo(NavigationEventArgs e)
{
if(NavigationContext.QueryString.ContainsKey("someValue"))
{
// string someValue = NavigationContext.QueryString["someValue"];
}
// OR
string someValue = string.Empty;
if (NavigationContext.QueryString.TryGetValue("someValue",out someValue))
{
// someValue contains the value
}
}