简体   繁体   English

如何检查NavigationContext.QueryString?

[英]How do I check for NavigationContext.QueryString?

In ASP.NET I can check for the existence of a QueryString key/value like so 在ASP.NET中,我可以像这样检查QueryString键/值的存在

if(Request.QueryString["someValue"] != null) 

However, I can't do this with NavigationContext.QueryString 但是,我无法使用NavigationContext.QueryString做到这一点

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

throws an error - The given key was not present in the dictionary 引发错误- The given key was not present in the dictionary

if(NavigationContext.QueryString.ContainsKey("someValue"))

Also throws an error. 也抛出错误。 This code is in the OnNavigatedTo method where it should be. 这段代码在OnNavigatedToOnNavigatedTo方法中的位置。

How do I check for the existence of a key / value in Windows Phone 8? 如何检查Windows Phone 8中是否存在键/值? My ugly, ugly workaround at the moment is to enclose each of these blocks in a try / catch with no code in the catch block. 我目前的丑陋变通方法是将每个这些块封装在try / catch中,而catch块中没有代码。 If the key is present the code completes, if not it throws the error which is silently caught. 如果存在密钥,则代码完成,否则,将引发被静默捕获的错误。

By checking if the QueryString contains the key should work, maybe your not in a proper context. 通过检查QueryString是否包含键应该可以工作,也许您的条件不正确。 Else try to get the value. 否则,尝试获取价值。 But check if the error is on the extraction process and not by accessing the NavigationContext or the QueryString (maybe they are null). 但是,请检查错误是否在提取过程中,而不是通过访问NavigationContext或QueryString(可能为空)来进行。

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
    }
}

The if(NavigationContext.QueryString["someValue"] != null) should work just fine. if(NavigationContext.QueryString["someValue"] != null)应该可以正常工作。 When do you call it? 你什么时候叫 You should do it in OnNavigatedTo . 您应该在OnNavigatedTo If you use it in the contrustor the NavigationContext is null. 如果在委托方中使用它,则NavigationContext为null。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM