简体   繁体   English

将控制器添加到所有页面UWP

[英]Add controller to all pages UWP

How to add an UI controller to all pages of the app? 如何将UI控制器添加到应用程序的所有页面? For example, having the same SplitView controller with navigation menu in Pane on all pages without copying its xaml code? 例如,是否在所有页面的窗格中都具有相同的SplitView控制器和导航菜单,而无需复制其xaml代码? Or maybe changing App.xaml in some way? 或者也许以某种方式更改App.xaml?

Or, as a second option, is it possible to create a UserControl that contains SplitView and put all other views on this page inside it Content? 或者,作为第二个选项,是否可以创建一个包含SplitView的UserControl并将此页面上的所有其他视图放入其中? I mean, how to put views into the UserControl in xaml (or even in code)? 我的意思是,如何将视图放入xaml(甚至代码)的UserControl中?

It sounds like you're after something like what Jerry Nixon suggests in this article here . 听起来您正在追寻杰里•尼克松(Jerry Nixon)在本文中建议的东西。

The essential idea is that instead of a Frame control hosting all of your app's content, you create a "Shell" (in the article's case, made of a SplitView, but really, it could probably be anything), which has some content of its own, as well as a Frame control (which then hosts the rest of your content). 基本思想是,您无需创建托管所有应用程序内容的Frame控件,而是创建一个“ Shell”(在本文中,它是由SplitView制成的,但实际上可能是任何东西),其中包含一些内容拥有一个, 以及一个Frame控件(该控件将托管您的其余内容)。

Create custom RootControl which derives from UserControl class and which contains the root Frame 创建自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>

Rewrite OnLauched : 重写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();
    }

and you can manage you actions from code-behind or ViewModel for navigation and other actions 并且您可以从代码隐藏或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 the sample and look how it works 下载样本并查看其工作方式

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM