2016-08-08 44 views
0

如何將UI控制器添加到應用程序的所有頁面?例如,在所有頁面上在窗格中使用與導航菜單相同的SplitView控制器而不復制其xaml代碼?或者可能以某種方式更改App.xaml?將控制器添加到所有頁面UWP

或者,作爲第二種選擇,是否可以創建一個包含SplitView的UserControl,並將該頁面上的所有其他視圖放入其Content?我的意思是,如何將視圖放入xaml中的用戶控件(甚至是代碼中)?

回答

1

創建自定義RootControlUserControl類派生幷包含根Frame

<SplitView DisplayMode="CompactOverlay"> 
     <SplitView.Pane> 
     <StackPanel> 
      <AppBarButton Icon="Home" 
          Width="50" 
          MinWidth="50" 
          Click="OnHomeClicked" 
          /> 
      <AppBarButton Icon="Shop" 
          Width="50" 
          MinWidth="50" 
          Click="OnShopClicked"/> 
      <AppBarButton Icon="Setting" 
          MinWidth="50" 
          Width="50" 
          Click="OnSettingsClicked"/> 
     </StackPanel> 
     </SplitView.Pane> 
     <SplitView.Content> 
     <Grid> 
      <Frame x:Name="rootFrame"/> 

      <!--Put your hamburger button here--> 

     </Grid> 
     </SplitView.Content> 
    </SplitView> 

重寫OnLauched

protected override void OnLaunched(LaunchActivatedEventArgs e) 
    { 

     var rootControl = Window.Current.Content as RootControl; 

     if (rootControl == null) 
     { 
      // Create a Frame to act as the navigation context and navigate to the first page 
      rootControl = new RootControl(); 

      rootControl.RootFrame.NavigationFailed += OnNavigationFailed; 

      if (e.PreviousExecutionState == ApplicationExecutionState.Terminated) 
      { 
       //TODO: Load state from previously suspended application 
      } 

      // Place the frame in the current Window 
      Window.Current.Content = rootControl; 
     } 

     if (rootControl.RootFrame.Content == null) 
     { 
      // When the navigation stack isn't restored navigate to the first page, 
      // configuring the new page by passing required information as a navigation 
      // parameter 
      rootControl.RootFrame.Navigate(typeof(MainPage), e.Arguments); 
     } 
     // Ensure the current window is active 
     Window.Current.Activate(); 
    } 

,你可以管理你的代碼隱藏或ViewModel採取行動導航等動作

public sealed partial class RootControl : UserControl 
    { 
     private Type currentPage; 

     public RootControl() 
     { 
      this.InitializeComponent(); 
      RootFrame.Navigated += OnNavigated; 
     } 

     private void OnNavigated(object sender, Windows.UI.Xaml.Navigation.NavigationEventArgs e) 
     { 
      currentPage = e.SourcePageType; 
     } 

     public Frame RootFrame 
     { 
      get 
      { 
       return rootFrame; 
      } 
     } 

     private void OnHomeClicked(object sender, Windows.UI.Xaml.RoutedEventArgs e) 
     { 
      Navigate(typeof(MainPage)); 
     } 

     private void OnShopClicked(object sender, Windows.UI.Xaml.RoutedEventArgs e) 
     { 
      Navigate(typeof(StorePage)); 
     } 

     private void OnSettingsClicked(object sender, Windows.UI.Xaml.RoutedEventArgs e) 
     { 
      Navigate(typeof(SettingsPage)); 
     } 

     private void Navigate(Type pageSourceType) 
     { 
      if (currentPage != pageSourceType) 
      { 
       RootFrame.Navigate(pageSourceType); 
      } 
     } 
    } 

Download樣品,看看它是如何工作

+0

嗨 感覺就像是你的榜樣是我需要的,但你的下載鏈接不起作用 –

+0

再試一次,我編輯鏈接 –

+0

現在的作品,非常感謝 –

1

聽起來好像你是在類似於傑里尼克鬆在this article here中所建議的那樣。

最基本的想法是,你不是創建一個「殼」(在文章的情況下,由SplitView構成,但實際上它可能是任何東西),而不是一個Frame控件託管所有應用程序的內容它自己的一些內容,以及一個框架控件(然後託管你的其餘內容)。

相關問題