0

我正在做一個Windows Store應用,對用戶輸入的請求,然後產生一束基於該輸入圖釘的頁面之間的導航的Bing Maps圖釘的狀態。當點擊圖釘時,應用程序將導航到更詳細的頁面。Windows應用商店的應用程序,保存和載入的

現在我遇到的問題是這樣的: 我的網頁全部來自自動生成的LayoutAwarePage繼承,所以我可能會使用即時存檔和LoadState的保存圖釘,使他們沒有得到關於導航擦拭。問題是,我無法將引腳保存到SaveState提供的Dictionary對象中。

我得到的錯誤是「值不能爲空」,它指的是LayoutAwarePage.OnNavigatedFrom()中的_pageKey變量,我不知道它爲什麼會發生。

我已經試過連載到JSON字符串,所以我可以在LoadState的deserialise它,但我得到使用字符串或UIElement的列表相同的結果。

我覺得這一切是由於我缺乏的是如何即時存檔,LayoutAwarePAge和SuspensionManager工作的認識。我以爲我在做什麼會工作,因爲字典只是要求一個字符串和一個對象。

我不使用從LayoutAwarePage任何其他方法,所以,如果有比使用即時存檔和LoadState一個更好的辦法,我所有的耳朵。

這些都是即時存檔的兩個版本我已​​經試過:

使用JSON

protected override void SaveState(Dictionary<String, Object> pageState) 
    { 
     List<string> pindata = new List<string>(); 
     List<string> serialisedpins = new List<string>(); 
     foreach (Pushpin ele in map.Children) 
     { 
      pindata = ele.Tag as List<string>; 
      serialisedpins.Add(JsonConvert.SerializeObject(pindata)); 
     } 

     string jasoned = JsonConvert.SerializeObject(serialisedpins); 
     pageState["pins"] = jasoned; 
    } 

使用的UIElement

protected override void SaveState(Dictionary<String, Object> pageState) 
    { 
     List<UIElement> pins = new List<UIElement>(map.Children); 
     pageState["pins"] = pins; 
    } 

回答

1

的錯誤你得到的名單(_pagekey值不能爲空)與您保存到Dictionary中的內容無關。唯一的例外是最有可能被扔在OnNavigateFrom()方法LayoutAwarePage

protected override void OnNavigatedFrom(NavigationEventArgs e) 
{ 
    var frameState = SuspensionManager.SessionStateForFrame(this.Frame); 
    var pageState = new Dictionary<String, Object>(); 
    this.SaveState(pageState); 
    frameState[_pageKey] = pageState; // <-- throws exception because _pageKey is null 
} 

如果你看一看的LayoutAwarePage的代碼的其餘部分,你會發現的_pageKey值在LayoutAwarePageOnNavigatedTo方法的設置:

protected override void OnNavigatedTo(NavigationEventArgs e) 
{ 
    // Returning to a cached page through navigation shouldn't trigger state loading 
    if (this._pageKey != null) return; 

    var frameState = SuspensionManager.SessionStateForFrame(this.Frame); 
    this._pageKey = "Page-" + this.Frame.BackStackDepth; <-- this line sets the _pageKey value 

    if (e.NavigationMode == NavigationMode.New) 
    { 
     // Clear existing state for forward navigation when adding a new page to the 
     // navigation stack 
     var nextPageKey = this._pageKey; 
     int nextPageIndex = this.Frame.BackStackDepth; 
     while (frameState.Remove(nextPageKey)) 
     { 
      nextPageIndex++; 
      nextPageKey = "Page-" + nextPageIndex; 
     } 

     // Pass the navigation parameter to the new page 
     this.LoadState(e.Parameter, null); 
    } 
    else 
    { 
     // Pass the navigation parameter and preserved page state to the page, using 
     // the same strategy for loading suspended state and recreating pages discarded 
     // from cache 
     this.LoadState(e.Parameter, (Dictionary<String, Object>)frameState[this._pageKey]); 
    } 
} 

通常其中的原因是,你在重寫自己的網頁OnNavigatedTo不調用base.OnNavigatedTo(e)裏面。覆蓋它應該始終的基本格局:

protected override void OnNavigatedTo(NavigationEventArgs e) 
{ 
    base.OnNavigatedTo(e); 
    // the rest of your own code 
} 

這將確保該基地的實施將執行並設置_pageKey值以及呼叫LoadState()如果有任何加載以前保存的狀態。

+0

謝謝 我知道varaible是導致該錯誤以及,我只是沒有意識到的OnNavigatedTo已被覆蓋。 發生了什麼事是我創建的項目爲空白頁,然後將其改爲繼承形式LayoutAwarePage而非頁,所以方法的OnNavigatedTo已經在那裏,沒有改變。 –

+0

我有同樣的問題。但我打電話'base.OnNavigatedTo(e)' –

+0

@SaadAnees你的'_pageKey'值是'null'?那麼,如果你打電話給'基地。OnNavigatedTo',嘗試單步執行該方法以確定其值將保持爲空的原因。 –

相關問題