簡體   English   中英

Windows 10 Univeral App - 標題欄中的按鈕不起作用

[英]Windows 10 Univeral App - Button in title bar not working

我正在嘗試在標題欄中添加一個按鈕。 我的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);
}

該按鈕位於應用程序的主體中,並且單擊事件正常工作。 我錯過了什么?

感謝名單,

我正在嘗試在標題欄中添加一個按鈕。 該按鈕顯示但不響應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>

然后, 將標題欄設置為矩形,而不是后面的代碼中的整個網格:

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);
}

以下是官方標題欄樣本供您參考,以下是上述測試代碼的輸出: 在此輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM