Win10的UWP之标题栏的返回键(二)

关于Win10的UWP的返回键的第二种处理的方法,是介于标题栏的强行修改,不是像上期的那样直接调用系统内置的API。
- - - - - - - - - - - - - - - - - - - - - - - -我是万恶的分割线- - - - - - - - - - - - - - - - - -
首先我们先设置以下界面的布局,界面的代码如下

        <StackPanel>
            <Grid x:Name="GridTitleBar" Background="SteelBlue" Height="36">
                <Grid  Background="Transparent">
                    <TextBlock Text="标题栏测试" 
                               HorizontalAlignment="Center" 
                               VerticalAlignment="Center"
                               FontSize="24"/>
                </Grid>
            </Grid>
            <Button x:Name="NexButton" 
                    Grid.Column="5" Grid.Row="5" 
                    Content="下一页" Click="NexButton_Click" />
        </StackPanel>

Win10的UWP之标题栏的返回键(二)

紧接着我们来处理以下界面的后台的代码,现在系统内原有的主方法写以下代码

        public MainPage()
        {
            this.InitializeComponent();
            if (App.IsHardwareButtonAPIPresent)
            {
                GridTitleBar.Visibility = Visibility.Collapsed;
                //MGridTitlr.Visibility = Visibility.Visible;
            }
            else
            {
                GridTitleBar.Visibility = Visibility.Visible;
                //MGridTitlr.Visibility = Visibility.Collapsed;
            }

Win10的UWP之标题栏的返回键(二)

这里我们来设置一下强行修改的标题栏的颜色,以及相关的属性设置

void TitleBar()
        {
            var coreTitleBar = CoreApplication.GetCurrentView().TitleBar;
                coreTitleBar.ExtendViewIntoTitleBar = true;
            Window.Current.SetTitleBar(GridTitleBar);

            var view = ApplicationView.GetForCurrentView();
            view.TitleBar.ButtonBackgroundColor = Colors.SteelBlue;
            view.TitleBar.ButtonForegroundColor = Colors.White;

            view.TitleBar.ButtonHoverBackgroundColor = Color.FromArgb(255, 92, 157, 211);
            view.TitleBar.ButtonHoverBackgroundColor = Colors.White;

            view.TitleBar.ButtonPressedBackgroundColor = Color.FromArgb(255, 92, 157, 211);
            view.TitleBar.ButtonPressedForegroundColor = Colors.White;

            view.TitleBar.ButtonInactiveBackgroundColor = Color.FromArgb(129, 70, 130, 180);
            view.TitleBar.ButtonInactiveForegroundColor = Colors.WhiteSmoke;

            Window.Current.Activated += (sender, args) =>
            {
                if (args.WindowActivationState != CoreWindowActivationState.CodeActivated)
                {
                    GridTitleBar.Opacity = 1;
                }
                else
                {
                    GridTitleBar.Opacity = 0.5;
                }
            };
        }

Win10的UWP之标题栏的返回键(二)

在来我们来处理一下界面中的跳转事件

        private void NexButton_Click(object sender, RoutedEventArgs e)
        {
            Frame.Navigate(typeof(BlankPage1));
        }

Win10的UWP之标题栏的返回键(二)

然后我们再来这里新建一个页面,来把响应的返回键放在修改过的标题栏上,由于是放回的主界面,除了主界面以外我们都不需要标题栏的返回键,这也是较为完美的一种处理方案。
Win10的UWP之标题栏的返回键(二)
我们再来处理以下新建页面的界面代码

<Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>

        <Grid Background="SteelBlue" Grid.Row="0" Height="36">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>
            <Grid x:Name="GridBackBtnBar" Grid.Column="0" Background="Transparent">
                <Button x:Name="BackButton" Click="BackButton_Click" 
                        Style="{StaticResource NavigationBackButtonSmallStyle}" />
            </Grid>
            <Grid x:Name="GridTitleBar" Grid.Column="1" Background="Transparent">
                <TextBlock Text="新的标题测试" 
                           HorizontalAlignment="Center"
                           VerticalAlignment="Center"
                            FontSize="24"/>
            </Grid>
        </Grid>

Win10的UWP之标题栏的返回键(二)

后面的我们再来相应的处理以下新建页面的后台代码

        void EnableBackButtonOnTitleBar(EventHandler<BackRequestedEventArgs> onBackRequested)
        {
            var currentView = SystemNavigationManager.GetForCurrentView();
            currentView.AppViewBackButtonVisibility = AppViewBackButtonVisibility.Visible;
            currentView.BackRequested += onBackRequested;
        }

        void TitleBar()
        {
            var coreTitleBar = CoreApplication.GetCurrentView().TitleBar;
            coreTitleBar.ExtendViewIntoTitleBar = true;
            Window.Current.SetTitleBar(GridTitleBar);

            var view = ApplicationView.GetForCurrentView();
            view.TitleBar.ButtonBackgroundColor = Colors.SteelBlue;
            view.TitleBar.ButtonForegroundColor = Colors.White;

            view.TitleBar.ButtonHoverBackgroundColor = Color.FromArgb(255, 92, 157, 211);
            view.TitleBar.ButtonHoverBackgroundColor = Colors.White;

            view.TitleBar.ButtonPressedBackgroundColor = Color.FromArgb(255, 92, 157, 211);
            view.TitleBar.ButtonPressedForegroundColor = Colors.White;

            view.TitleBar.ButtonInactiveBackgroundColor = Color.FromArgb(129, 70, 130, 180);
            view.TitleBar.ButtonInactiveForegroundColor = Colors.WhiteSmoke;

            Window.Current.Activated += (sender, args) =>
            {
                if (args.WindowActivationState != CoreWindowActivationState.CodeActivated)
                {
                    GridTitleBar.Opacity = 1;
                }
                else
                {
                    GridTitleBar.Opacity = 0.5;
                }
            };
        }

Win10的UWP之标题栏的返回键(二)

由于我们在前面做了Click事件,现在我们把Click事件处理了

        private void BackButton_Click(object sender, RoutedEventArgs e)
        {
            if (this.Frame.CanGoBack)
            {
                this.Frame.GoBack();
            }
        }

Win10的UWP之标题栏的返回键(二)

写到这里也就差不多了,回头看看,我们还少写了一句代码,这是调用写好的方法在主方法中使用

  TitleBar();

Win10的UWP之标题栏的返回键(二)

好了,写了这么久,终于写完了,看看我们的目的达到了没有
Win10的UWP之标题栏的返回键(二)

想看的结果的页面是最后一张图,嘻嘻!!!
Win10的UWP之标题栏的返回键(二)