微軟UWP應用,導航欄設計。
阿新 • • 發佈:2019-05-02
soft .text key pre exceptio ext lln exception sea
實現兩個頁面之間的導航
重要的 API:Windows.UI.Xaml.Controls.Frame 類, Windows.UI.Xaml.Controls.Page 類 Windows.UI.Xaml.Navigation命名空間
在C#的通用桌面應用中加入2個空白頁面:Page1,Page2.
<Grid> <TextBlock x:Name="pageTitle" Text="Page1" /> <StackPanel> <TextBlock HorizontalAlignment="Center" Text="Enter your name"/> <TextBox HorizontalAlignment="Center" Width="200" Name="name"/> <HyperlinkButton Content="Click to go to page 2" Click="HyperlinkButton_Click" HorizontalAlignment="Center" RenderTransformOrigin="0.475,3.859" VerticalAlignment="Center"/> </StackPanel> </Grid>
在Page1.xaml中加入HyperlinkButton元素,添加Click事件轉至Page2。
<Grid> <TextBlock x:Name="pageTitle" Text="Page2" /> <StackPanel> <TextBlock HorizontalAlignment="Center"Name="greeting"/> <HyperlinkButton Content="Click to go to page 1" Click="HyperlinkButton_Click" HorizontalAlignment="Center"/> </StackPanel> <Button x:Name="BackButton" VerticalAlignment="Top" HorizontalAlignment="Left" Click="Back_Click" Style="{StaticResource NavigationBackButtonNormalStyle}"/> </Grid>
public sealed partial class Page1 : Page { public Page1() { this.InitializeComponent(); this.NavigationCacheMode = Windows.UI.Xaml.Navigation.NavigationCacheMode.Enabled; } private void HyperlinkButton_Click(object sender, RoutedEventArgs e) { this.Frame.Navigate(typeof(Page2), name.Text); } }
public sealed partial class Page2 : Page { public Page2() { this.InitializeComponent(); } private void HyperlinkButton_Click(object sender, RoutedEventArgs e) { this.Frame.Navigate(typeof(Page1)); } protected override void OnNavigatedTo(NavigationEventArgs e) { BackButton.IsEnabled = this.Frame.CanGoBack; if (e.Parameter is string && !string.IsNullOrWhiteSpace((string)e.Parameter)) { greeting.Text = $"Hi, {e.Parameter.ToString()}"; } else { greeting.Text = "Hi!"; } base.OnNavigatedTo(e); } private void Back_Click(object sender, RoutedEventArgs e) { On_BackRequested(); } // Handles system-level BackRequested events and page-level back button Click events private bool On_BackRequested() { if (this.Frame.CanGoBack) { this.Frame.GoBack(); return true; } return false; } private void BackInvoked(KeyboardAccelerator sender, KeyboardAcceleratorInvokedEventArgs args) { On_BackRequested(); args.Handled = true; } }
App.xaml.cs代碼如下:
sealed partial class App : Application { /// <summary> /// 初始化單一實例應用程序對象。這是執行的創作代碼的第一行, /// 已執行,邏輯上等同於 main() 或 WinMain()。 /// </summary> public App() { this.InitializeComponent(); this.Suspending += OnSuspending; } /// <summary> /// 在應用程序由最終用戶正常啟動時進行調用。 /// 將在啟動應用程序以打開特定文件等情況下使用。 /// </summary> /// <param name="e">有關啟動請求和過程的詳細信息。</param> protected override void OnLaunched(LaunchActivatedEventArgs e) { Frame rootFrame = Window.Current.Content as Frame; // 不要在窗口已包含內容時重復應用程序初始化, // 只需確保窗口處於活動狀態 if (rootFrame == null) { // 創建要充當導航上下文的框架,並導航到第一頁 rootFrame = new Frame(); rootFrame.NavigationFailed += OnNavigationFailed; if (e.PreviousExecutionState == ApplicationExecutionState.Terminated) { //TODO: 從之前掛起的應用程序加載狀態 } // 將框架放在當前窗口中 Window.Current.Content = rootFrame; } if (e.PrelaunchActivated == false) { if (rootFrame.Content == null) { // 當導航堆棧尚未還原時,導航到第一頁, // 並通過將所需信息作為導航參數傳入來配置 // 參數 rootFrame.Navigate(typeof(Page1), e.Arguments); } // 確保當前窗口處於活動狀態 Window.Current.Activate(); } } /// <summary> /// 導航到特定頁失敗時調用 /// </summary> ///<param name="sender">導航失敗的框架</param> ///<param name="e">有關導航失敗的詳細信息</param> void OnNavigationFailed(object sender, NavigationFailedEventArgs e) { throw new Exception("Failed to load Page " + e.SourcePageType.FullName); } /// <summary> /// 在將要掛起應用程序執行時調用。 在不知道應用程序 /// 無需知道應用程序會被終止還是會恢復, /// 並讓內存內容保持不變。 /// </summary> /// <param name="sender">掛起的請求的源。</param> /// <param name="e">有關掛起請求的詳細信息。</param> private void OnSuspending(object sender, SuspendingEventArgs e) { var deferral = e.SuspendingOperation.GetDeferral(); //TODO: 保存應用程序狀態並停止任何後臺活動 deferral.Complete(); } }
運行結果:
微軟UWP應用,導航欄設計。