- 導航歷史記錄管理,起始導航歷史記錄是用於一個導航堆棧來管理的。如完成以下導航:MainPage ->Page1 ->Page 2 ->Page 3,其實就形成了一個如下圖的導航堆棧:
所有當按“返回鍵”時也是按後進先出得原則進行導航,但是思考如下問題:
- 它的原理是什麼呢?每個應用程序都有一個 RootFrame。當用戶導航到該頁面時,導航框架會將應用程序的每個頁面或 PhoneApplicationPage 的實例設置爲框架的Content,同時RootFrame有一個RootFrame.BackStack。
- 它是怎樣去管理和存儲這個頁面歷史記錄的呢?RootFrame.BackStack類似一個堆棧的操作,它存儲歷史記錄中的條目(JournalEntry)類型。
- 是否可以去控制和操作這個堆棧呢?當然是可以去控制這個堆棧,如同我們去操作一個堆棧的數據結構一樣,它也有Pop(OS進行操作)和Push操作,push是用RootFrame.RemoveBackEntry()來完成的。
// The BackStack property is a collection of JournalEntry objects.
foreach (JournalEntry journalEntry in RootFrame.BackStack.Reverse())
{
historyListBox.Items.Insert(0, i + ": " + journalEntry.Source);
i++;
}
// If RemoveBackEntry is called on an empty back stack, an InvalidOperationException is thrown.
// Check to make sure the BackStack has entries before calling RemoveBackEntry.
if (RootFrame.BackStack.Count() > 0)
RootFrame.RemoveBackEntry();
2. 怎樣管理開始頁中的磁條?
我們在頁面中添加一個checkbox,當選中的時候,此頁就洗到開始頁中,否則從開始頁中取出。
/// <summary>
/// Toggle pinning a Tile for this page on the Start screen.
/// </summary>
private void PinToStartCheckBox_Click(object sender, RoutedEventArgs e)
{
// Try to find a Tile that has this page's URI.
ShellTile tile = ShellTile.ActiveTiles.FirstOrDefault(o => o.NavigationUri.ToString().Contains(NavigationService.Source.ToString()));
if (tile == null)
{
// No Tile was found, so add one for this page.
StandardTileData tileData = new StandardTileData { Title = PageTitle.Text };
ShellTile.Create(new Uri(NavigationService.Source.ToString(), UriKind.Relative), tileData);
}
else
{
// A Tile was found, so remove it.
tile.Delete();
}
}