- 导航历史记录管理,起始导航历史记录是用于一个导航堆栈来管理的。如完成以下导航: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();
}
}