Windows 10 Univeral App - 标题栏中的按钮不起作用

时间:2016-06-15 16:59:53

标签: c# xaml windows-10-universal

我正在尝试在标题栏中添加一个按钮。我的XAML如下所示:

<Page
    x:Class="FullScreen.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:FullScreen"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>

        <Grid x:Name="TitleBar">
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto"/>
                    <ColumnDefinition Width="Auto"/>
                </Grid.ColumnDefinitions>
                <TextBlock Grid.Column="0" Text="Application Name" VerticalAlignment="Center"/>
                <Button Grid.Column="1" Content="Test" Click="Button_Click"/>
            </Grid>
        </Grid>

        <Grid Grid.Row="1">
            <TextBlock>Content</TextBlock>
        </Grid>
    </Grid>
</Page>

.cs页面包含以下代码:

using System;
using Windows.ApplicationModel.Core;
using Windows.UI.Popups;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

namespace FullScreen
{
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            InitializeComponent();

            CoreApplication.GetCurrentView().TitleBar.ExtendViewIntoTitleBar = true;
            Window.Current.SetTitleBar(TitleBar);
        }

        private async void Button_Click(object sender, RoutedEventArgs e)
        {
            await new MessageDialog("Click!").ShowAsync();
        }
    }
}

按钮显示但不响应click事件。如果我在构造函数中注释掉两行:

public MainPage()
{
    InitializeComponent();

    //CoreApplication.GetCurrentView().TitleBar.ExtendViewIntoTitleBar = true;
    //Window.Current.SetTitleBar(TitleBar);
}

该按钮位于应用程序的主体中,点击事件正常工作。我错过了什么?

感谢名单,

1 个答案:

答案 0 :(得分:7)

  

我试图在标题栏中添加一个按钮。该按钮显示但不响应点击事件。

根据官方文档(Window.SetTitleBar的备注部分),此行为是设计使然。

  

<强>输入
  当您调用此方法将XAML UIElement设置为标题栏时,它允许Windows处理标题栏UIElement的输入,其处理方式与处理默认系统标题栏的输入相同。例如,用户可以通过拖动XAML UIElement来移动窗口,或通过右键单击来调用窗口上下文菜单。
  这意味着当用户使用触摸,鼠标或笔与目标UIElement或其子项进行交互时,您的应用不再接收指针输入。但是,您必须仍处理(或阻止)键盘输入,并确定标题栏中的内容是否可以通过键盘以标记来获得焦点

为了让标题栏中的按钮响应点击事件,我们可以首先在xaml页面中为自定义标题栏添加一个矩形

<Grid x:Name="TitleBar">
    <!--Add a rectangle here-->
    <Rectangle x:Name="BackgroundElement" />
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition Width="Auto" />
        </Grid.ColumnDefinitions>
        <TextBlock Grid.Column="0" Text="Application Name" VerticalAlignment="Center" />
        <Button Grid.Column="1" Content="Test" Click="Button_Click" />
    </Grid>
</Grid>

然后,将标题栏设置为矩形,而不是后面代码中的整个网格:

public MainPage()
{
    InitializeComponent();

    CoreApplication.GetCurrentView().TitleBar.ExtendViewIntoTitleBar = true;
    // Set TitleBar to BackgroundElement instead of the entire grid
    // Clicks on the BackgroundElement will be treated as clicks on the title bar.
    Window.Current.SetTitleBar(BackgroundElement);
}

以下是供您参考的官方Title bar sample,以下是上述测试代码的输出: enter image description here