背水一戰 Windows 10 之 控制項(導航類): Frame ...
背水一戰 Windows 10 (41) - 控制項(導航類): Frame
作者:webabcd
介紹
背水一戰 Windows 10 之 控制項(導航類)
- Frame
示例
Controls/NavigationControl/FrameDemo.xaml
<Page x:Class="Windows10.Controls.NavigationControl.FrameDemo" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:Windows10.Controls.NavigationControl" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"> <Grid Background="Transparent"> <StackPanel Margin="10 0 10 10" Orientation="Horizontal"> <StackPanel Width="400"> <Button Name="btnGotoFrame1" Content="導航至 Frame1" Click="btnGotoFrame1_Click" /> <Button Name="btnGotoFrame2" Content="導航至 Frame2" Click="btnGotoFrame2_Click" Margin="0 10 0 0" /> <Button Name="btnBack" Content="後退" Click="btnBack_Click" Margin="0 10 0 0" /> <Button Name="btnForward" Content="前進" Click="btnForward_Click" Margin="0 10 0 0" /> <TextBlock Name="lblMsg" TextWrapping="Wrap" Margin="0 10 0 0" /> </StackPanel> <Frame Name="frame" VerticalAlignment="Top" Margin="10 0 0 0" /> </StackPanel> </Grid> </Page>
Controls/NavigationControl/FrameDemo.xaml.cs
/* * Frame - 框架控制項,用於導航內容(繼承自 ContentControl, 請參見 /Controls/BaseControl/ContentControlDemo/) * BackStackDepth - 返回 stack 中的條目數 * BackStack - 返迴向後導航歷史記錄的 PageStackEntry 實例的集合(可以根據需求對集合增刪改查) * ForwardStack - 返迴向前導航歷史記錄的 PageStackEntry 實例的集合(可以根據需求對集合增刪改查) * CanGoBack - 可否可以向後導航 * CanGoForward - 可否可以向前導航 * GoBack() - 向後導航,可以指定過渡效果 * GoForward() - 向前導航 * Navigate() - 導航到指定的 Type,可以傳遞一個 object 類型的參數,可以指定過渡效果 * CurrentSourcePageType - 獲取 Frame 當前內容的 Type * SourcePageType - 獲取或設置 Frame 當前內容的 Type * * CacheSize - 所支持的最大緩存頁數,預設值 10 * CacheSize 與被導航的頁的 Page.NavigationCacheMode 屬性相關(詳見 Frame1.xaml.cs 和 Frame2.xaml.cs 的示例代碼) * NavigationCacheMode.Disabled - 每次導航到頁時,都重新實例化此頁,預設值(CacheSize 無效) * NavigationCacheMode.Enabled - 每次導航到頁時,首先緩存此頁,此時如果已緩存的頁數大於 CacheSize,則按先進先出的原則丟棄最早的緩存頁(CacheSize 有效) * NavigationCacheMode.Required - 每次導航到頁時,都緩存此頁(CacheSize 無效) * * Navigating - 導航開始時觸發的事件 * Navigated - 導航完成後觸發的事件 * NavigationFailed - 導航失敗時觸發的事件 * NavigationStopped - 導航過程中,又請求了一個新的導航時觸發的事件 * * GetNavigationState() - 獲取 Frame 當前的導航狀態,返回字元串類型的數據,僅當導航無參數傳遞或只傳遞簡單參數(int, char, string, guid, bool 等)時有效 * SetNavigationState(string navigationState) - 將 Frame 還原到指定的導航狀態 * * * PageStackEntry - 保存在 stack 中的頁對象 * SourcePageType - 獲取此頁的類型 * Parameter - 獲取之前導航至此頁時的參數 * * * NavigationEventArgs - 導航的事件參數 * NavigationMode - 導航方式,只讀(Windows.UI.Xaml.Navigation.NavigationMode 枚舉) * New, Back, Forward, Refresh * Parameter - 傳遞給導航目標頁的參數,只讀 * SourcePageType - 導航的目標頁的類型,只讀 * * * 註: * 1、關於導航過程中的過渡效果請參見 /Animation/ThemeTransition/NavigationTransitionInfo/ 中的內容 * 2、Frame 中與過渡效果有關的是 GoBack() 和 Navigate() 中的 NavigationTransitionInfo 類型的參數 */ using System; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Navigation; namespace Windows10.Controls.NavigationControl { public sealed partial class FrameDemo : Page { public FrameDemo() { this.InitializeComponent(); } protected override void OnNavigatedTo(NavigationEventArgs e) { frame.Navigated += frame_Navigated; } void frame_Navigated(object sender, NavigationEventArgs e) { lblMsg.Text = "CacheSize: " + frame.CacheSize; lblMsg.Text += Environment.NewLine; lblMsg.Text += "BackStackDepth: " + frame.BackStackDepth; lblMsg.Text += Environment.NewLine; lblMsg.Text += "CanGoBack: " + frame.CanGoBack; lblMsg.Text += Environment.NewLine; lblMsg.Text += "CanGoForward: " + frame.CanGoForward; lblMsg.Text += Environment.NewLine; lblMsg.Text += "CurrentSourcePageType: " + frame.CurrentSourcePageType; lblMsg.Text += Environment.NewLine; // 顯示 frame 的當前的導航狀態,記錄此值後,可以在需要的時候通過 SetNavigationState() 將 frame 還原到指定的導航狀態 lblMsg.Text += "NavigationState: " + frame.GetNavigationState(); } private void btnGotoFrame1_Click(object sender, RoutedEventArgs e) { frame.Navigate(typeof(Frame1), "param1"); } private void btnGotoFrame2_Click(object sender, RoutedEventArgs e) { frame.SourcePageType = typeof(Frame2); } private void btnBack_Click(object sender, RoutedEventArgs e) { if (frame.CanGoBack) frame.GoBack(); } private void btnForward_Click(object sender, RoutedEventArgs e) { if (frame.CanGoForward) frame.GoForward(); } } }
Controls/NavigationControl/Frame1.xaml
<Page x:Class="Windows10.Controls.NavigationControl.Frame1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:Windows10.Controls.NavigationControl" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"> <Grid Background="Transparent"> <StackPanel Margin="10 0 10 10"> <TextBlock Name="lblMsg" Margin="5" TextWrapping="Wrap" /> </StackPanel> </Grid> </Page>
Controls/NavigationControl/Frame1.xaml.cs
using System; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Navigation; namespace Windows10.Controls.NavigationControl { public sealed partial class Frame1 : Page { public Frame1() { this.InitializeComponent(); /* * Page.NavigationCacheMode - 使用 Frame 導航到此頁面時,頁面的緩存模式 * Disabled - 每次導航到頁時,都重新實例化此頁,預設值(Frame.CacheSize 無效) * Enabled - 每次導航到頁時,首先緩存此頁,此時如果已緩存的頁數大於 Frame.CacheSize,則按先進先出的原則丟棄最早的緩存頁(Frame.CacheSize 有效) * Required - 每次導航到頁時,都緩存此頁(Frame.CacheSize 無效) */ this.NavigationCacheMode = Windows.UI.Xaml.Navigation.NavigationCacheMode.Enabled; this.Loaded += Frame1_Loaded; } void Frame1_Loaded(object sender, Windows.UI.Xaml.RoutedEventArgs e) { lblMsg.Text += Environment.NewLine; lblMsg.Text += "Loaded: " + DateTime.Now.ToString(); } // 來了 protected override void OnNavigatedTo(NavigationEventArgs e) { lblMsg.Text += Environment.NewLine; lblMsg.Text += "OnNavigatedTo: " + DateTime.Now.ToString(); lblMsg.Text += " param: " + (string)e.Parameter; } // 準備走了,但是可以取消 protected override void OnNavigatingFrom(NavigatingCancelEventArgs e) { lblMsg.Text += Environment.NewLine; lblMsg.Text += "OnNavigatingFrom(NavigatingCancelEventArgs): " + DateTime.Now.ToString(); lblMsg.Text += " param: " + (string)e.Parameter; } // 已經走了 protected override void OnNavigatedFrom(NavigationEventArgs e) { lblMsg.Text += Environment.NewLine; lblMsg.Text += "OnNavigatedFrom(NavigationEventArgs): " + DateTime.Now.ToString(); lblMsg.Text += " param: " + (string)e.Parameter; } } }
Controls/NavigationControl/Frame2.xaml
<Page x:Class="Windows10.Controls.NavigationControl.Frame2" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:Windows10.Controls.NavigationControl" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"> <Grid Background="Transparent"> <StackPanel Margin="10 0 10 10"> <TextBlock Name="lblMsg" Margin="5" TextWrapping="Wrap" /> </StackPanel> </Grid> </Page>
Controls/NavigationControl/Frame2.xaml.cs
using System; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Navigation; namespace Windows10.Controls.NavigationControl { public sealed partial class Frame2 : Page { public Frame2() { this.InitializeComponent(); /* * Page.NavigationCacheMode - 使用 Frame 導航到此頁面時,頁面的緩存模式 * Disabled - 每次導航到頁時,都重新實例化此頁,預設值(Frame.CacheSize 無效) * Enabled - 每次導航到頁時,首先緩存此頁,此時如果已緩存的頁數大於 Frame.CacheSize,則按先進先出的原則丟棄最早的緩存頁(Frame.CacheSize 有效) * Required - 每次導航到頁時,都緩存此頁(Frame.CacheSize 無效) */ this.NavigationCacheMode = Windows.UI.Xaml.Navigation.NavigationCacheMode.Enabled; this.Loaded += Frame2_Loaded; } void Frame2_Loaded(object sender, Windows.UI.Xaml.RoutedEventArgs e) { lblMsg.Text += Environment.NewLine; lblMsg.Text += "Loaded: " + DateTime.Now.ToString(); } protected override void OnNavigatedTo(NavigationEventArgs e) { lblMsg.Text += Environment.NewLine; lblMsg.Text += "OnNavigatedTo: " + DateTime.Now.ToString(); } } }
OK
[源碼下載]