2016-06-15 15 views
1

タイトルバーにボタンを追加しようとしています。私のXAMLは、次のようになります。Windows 10 Univeral App - タイトルバーのボタンが機能しない

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

ボタンが現れるが、それは、クリックイベントに応答しません。コンストラクタの2行をコメントアウトすると、

public MainPage() 
{ 
    InitializeComponent(); 

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

ボタンがアプリケーションの本体にあり、clickイベントが正しく機能します。私は何が欠けていますか?

ありがとう、

答えて

6

私は、タイトルバーにボタンを追加しようとしています。ボタンは表示されますが、クリックイベントに応答しません。

公式のドキュメント(備考の部分Window.SetTitleBar)によれば、この動作は仕様です。

入力
あなたはタイトルバーとしてXAMLのUIElementを設定するには、このメソッドを呼び出すと、それはWindowsがタイトルバーのUIElementには、デフォルトのシステムタイトルバーへの入力を処理するのと同じ方法で入力を処理することができます。たとえば、ユーザーはXAML UIElementをドラッグしてウィンドウを移動したり、ウィンドウのコンテキストメニューを右クリックして呼び出すことができます。
これは、ユーザがタッチ、マウス、またはペンを使用してターゲットUIElementまたはその子とやりとりするときに、というポインタ入力を受け取らないことを意味します。ただし、はまだキーボード入力を処理(または防止)し、タイトルバーのコンテンツにキーボードでというタブを付けてフォーカスを受け取ることができるかどうかを判断する必要があります。

<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> 

その後、

クリックイベントに対応するタイトルバー内のボタンを作るために

、我々は最初のXAMLページ内のカスタマイズタイトルバーのための長方形を追加することができます後ろにコード内の長方形の代わりに、グリッド全体にタイトルバーを設定します。ここでは

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

は公式ですを参照してください。上記のテストコードの出力は次のとおりです。 enter image description here

関連する問題