2013-09-29 16 views
2

在ASP.NET我可以檢查查詢字符串鍵/值的存在,像這樣如何檢查NavigationContext.QueryString?

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

但是,我不能NavigationContext.QueryString

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

這樣做會引發錯誤 - The given key was not present in the dictionary

還拋出一個錯誤。該代碼位於OnNavigatedTo方法中。

如何檢查Windows Phone 8的鍵/值的存在?我目前醜陋醜陋的解決方法是將每個塊封裝在try/catch中,catch塊中沒有代碼。如果密鑰存在,則代碼完成,否則它會拋出錯誤,這是默默捕捉的。

回答

0

if(NavigationContext.QueryString["someValue"] != null)應該只是罰款。你什麼時候打電話?你應該在OnNavigatedTo。如果您在控制器中使用它,則NavigationContext爲空。

+0

我檢查的OnNavigatedTo。我得到的錯誤是'給定的鍵沒有出現在字典中' – roryok

1

通過檢查查詢字符串中包含的關鍵應該工作,也許你不是在一個適當的範圍內。否則嘗試獲得價值。但檢查錯誤是否在提取過程中,而不是通過訪問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 
    } 
}