【问题标题】:Add controller to all pages UWP将控制器添加到所有页面 UWP
【发布时间】:2016-08-09 14:36:09
【问题描述】:

如何将 UI 控制器添加到应用的所有页面?例如,在所有页面的窗格中都有相同的带有导航菜单的 SplitView 控制器,而无需复制其 xaml 代码?或者可能以某种方式更改 App.xaml?

或者,作为第二个选项,是否可以创建一个包含 SplitView 的 UserControl 并将此页面上的所有其他视图放在它的 Content 中?我的意思是,如何在 xaml 中(甚至在代码中)将视图放入 UserControl 中?

【问题讨论】:

    标签: c# xaml windows-10 uwp


    【解决方案1】:

    创建派生自UserControl 类并包含根Frame 的自定义RootControl

        <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 示例,看看它是如何工作的

    【讨论】:

    【解决方案2】:

    听起来你在追求 Jerry Nixon 在this article here 中建议的东西。

    基本思想是,您创建一个“Shell”(在本文的例子中,由 SplitView 组成,但实际上,它可能是任何东西),而不是托管所有应用程序内容的 Frame 控件,它有一些它自己的内容,以及一个框架控件(然后托管您的其余内容)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-21
      • 2016-03-06
      • 1970-01-01
      相关资源
      最近更新 更多