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

use*_*543 2 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>
Run Code Online (Sandbox Code Playgroud)

.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();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

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

public MainPage()
{
    InitializeComponent();

    //CoreApplication.GetCurrentView().TitleBar.ExtendViewIntoTitleBar = true;
    //Window.Current.SetTitleBar(TitleBar);
}
Run Code Online (Sandbox Code Playgroud)

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

感谢名单,

小智 7

我正在尝试在标题栏中添加一个按钮.该按钮显示但不响应click事件.

根据官方文档(备注Window.SetTitleBar的一部分),这种行为是设计的.

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

为了使标题栏中的按钮响应click事件,我们可以首先在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>
Run Code Online (Sandbox Code Playgroud)

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

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);
}
Run Code Online (Sandbox Code Playgroud)

以下是官方标题栏样本供您参考,以下是上述测试代码的输出: 在此输入图像描述