2012-08-15 24 views
0

我在我的應用程序中實現了次要圖塊,因此用戶可以將次要圖塊固定到開始屏幕,然後相應地導航到我的應用程序中的相應頁面。這個實現工作正常,除了當用戶在從固定的次級磁貼導航到特定頁面後點擊後退硬件按鈕時,什麼都沒有發生?事實上,儘管用戶來自開始屏幕,但實際上只顯示應用程序中的上一頁。真正返回到開始屏幕的正確方法是什麼,因爲用戶會期望會發生(我認爲這將是適當的後退堆棧導航)?如何在從次要圖塊導航到頁面時管理返回堆棧

我有如下,但只適用於正常頁面導航的情況下,而不是當用戶從開始屏幕固定瓷磚導航到SharePage時。

MainPage.xaml.cs中

protected override void OnNavigatedTo(NavigationEventArgs e) 
    { 
     base.OnNavigatedTo(e); 

     string _title = null; 
     NavigationContext.QueryString.TryGetValue("Param", out _title); 

     if (_title != null) 
     { 
      switch (_title) 
      { 
       case "status": 
        this.NavigationService.Navigate(new Uri("/Views/ShareStatusPage.xaml", UriKind.Relative)); 
        break; 
       case "link": 
        this.NavigationService.Navigate(new Uri("/Views/ShareLinkPage.xaml", UriKind.Relative)); 
        break; 
      } 
     }    
    } 

private void CreateLiveTile(TileItem item) 
    { 
     string tileParameter = "Param=" + item.Title.ToString(); 

     ShellTile Tile = CheckIfTileExist(tileParameter); // Check if Tile's title has been used 

     if (Tile == null) 
     { 
      try 
      { 
       var LiveTile = new StandardTileData 
       { 
        Title = item.TileName, 
        //BackgroundImage = ((System.Windows.Media.Imaging.BitmapImage)hubtile.Source).UriSource, 
        BackgroundImage = new Uri(item.ImageUri.ToString(), UriKind.Relative), 
        //Count = 1, 
        BackTitle = item.TileName, 
        //BackBackgroundImage = new Uri("", UriKind.Relative), 
        BackContent = item.Message, 
       }; 

       ShellTile.Create(new Uri("/MainPage.xaml?" + tileParameter, UriKind.Relative), LiveTile); //pass the tile parameter as the QueryString 

      } 
      catch (Exception) 
      { 
       MessageBox.Show("This tile could not be pinned", "Warning", MessageBoxButton.OK); 
      } 
     } 
     else 
     { 
      MessageBox.Show("This tile has already been pinned", "Notice", MessageBoxButton.OK); 
     } 
    } 

private ShellTile CheckIfTileExist(string tileUri) 
    { 
     ShellTile shellTile = ShellTile.ActiveTiles.FirstOrDefault(tile => tile.NavigationUri.ToString().Contains(tileUri)); 
     return shellTile; 
    } 

SharePage.xaml.cs

protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e) 
    { 
     base.OnBackKeyPress(e); 

     //return to the previous page in the phones back stack? 
     if (NavigationService.CanGoBack) 
     { 
      e.Cancel = true; 
      NavigationService.GoBack(); 
     } 
     //else 
     //{ 
     // ?? 
     //} 
    } 

到目前爲止,CreateLiveTile()方法創建瓷磚二次,然後當按下該圖塊,被的MainPage導航到並然後在MainPage OnNavigatedTo事件中檢查查詢字符串,然後根據單擊的次級磁貼加載相應的頁面。一旦執行此操作並且各個頁面已經加載,我就不能再按回退按鈕返回到開始屏幕以遵循標準的堆疊行爲。我怎樣才能解決這個問題?

回答

0

根據你的新的更新,讓我們來看看這個方法:

private void CreateLiveTile(TileItem item) 
{ 
    string tileParameter = "Param=" + item.Title.ToString(); 
    //... 

    if (Tile == null) 
    { 
     try 
     { 
      var LiveTile = new StandardTileData 
      { 
       //... 
      }; 

      ShellTile.Create(new Uri("/MainPage.xaml?" + tileParameter, UriKind.Relative), LiveTile); //pass the tile parameter as the QueryString 

      //blah-blah-blah 
} 

在這裏,您創建一個新的瓷磚,並通過tileParameterMainPage所以,你導航到主頁面,然後檢測瓷磚參數的文本,並導航到ShareLinkShareStatus頁這就是爲什麼你有。一個骯髒的導航堆棧

讓我建議你的方式來避免這種情況:

private void CreateLiveTile(TileItem item) 
    { 

    var title = item.Title.ToString(); 
    //... 

    if (Tile == null) 
    { 
     try 
     { 
      var LiveTile = new StandardTileData 
      { 
       //... 
      }; 
      string page; 
     switch (title) 
     { 
      case "status": 
       page = "/Views/ShareStatusPage.xaml"; 
       break; 
      case "link": 
       page = "/Views/ShareLinkPage.xaml"); 
       break; 
     }     
     if(string.IsNullOrEmpty(page)) 
     { 
      //handle this situation. for example: page = "/MainPage.xaml"; 
     } 
      ShellTile.Create(new Uri(page, UriKind.Relative), LiveTile); 

      //blah-blah-blah 
} 

當用戶在您的次要瓷磚水龍頭,他將直接導航到ShareLinkShareStatus頁。並且NavigationStack將是乾淨的。當用戶按下Back按鈕時,應用程序將被關閉,用戶將看到一個開始屏幕(這是次要圖塊的右後退按鈕行爲)。

p.s.如果您有服務,請不要忘記啓動您的所有服務或加載所有資源。因爲MainPage不會被創建!無論如何,應用程序的每個頁面都必須能夠啓動整個應用程序,因爲您必須支持從tombstoned state進行恢復。

如果您需要,隨時諮詢詳情。

+0

@感謝您的優秀建議,它的效果非常好! – Matthew 2012-08-16 20:55:21

0

;否則可以使用NavigationService.Navigate(homepageUri)

+0

我不想使用它,因爲它可能會導致我的應用程序導航堆棧中某處出現不必要的循環。我編輯了我的原始帖子,以更準確地反映我的問題。 – Matthew 2012-08-15 22:00:32

0

你爲什麼要取消navigaion?請刪除OnBackKeyPress。在這種情況下你不需要它。

+0

如果頁面從我的應用程序導航到頁面,我有OnBackKeyPress,但在導航到活動磁貼的情況下,我不確定如何返回到用戶期望的開始屏幕?我在原來的帖子中添加了更多,也許這會有所幫助? – Matthew 2012-08-15 22:01:13

0

創建一個變量來跟蹤導航到主頁面是否來自通過次要圖塊的導航。在MainPage.Load上檢查該變量,並且如果該變量是通過輔助磁貼來的,則從後面的堆棧中移除上一頁。離開主頁面回到開始菜單是有意義的,而不是回到次要的頁面頁面。這是怎麼MyStocks組合(一個也沒有我的應用程序,但一個I L

這裏有後退按鈕使用一個很好的博客: http://blogs.msdn.com/b/ptorr/archive/2011/10/06/back-means-back-not-forwards-not-sideways-but-back.aspx

+0

我編輯了我的解決方案,以更準確地展示我的實現和上述問題。也許這會讓人更深入瞭解如何糾正問題? – Matthew 2012-08-15 21:59:46

相關問題