【问题标题】:Right clicking a button inflates AppBar instead of firing right click event右键单击按钮会使 AppBar 膨胀而不是触发右键单击事件
【发布时间】:2017-02-24 18:08:50
【问题描述】:

所以我的 AppBar 中有一个按钮,可以将按钮添加到位于页面网格中的堆栈面板。 我为新按钮分配了一个 RightTapped 事件。 但是,当我右键单击一个新按钮时,程序不会触发我分配给 RightTapped 事件的方法,而是使 AppBar 膨胀。 我尝试将 IsRightTapEnabled="False" 除了新按钮之外的每个项目都设置一个,但这对问题没有帮助。 我被困住了,我需要帮助。

这是我的代码:

public sealed partial class MainPage : Page
{
    public MainPage()
    {
        this.InitializeComponent();
    }
    int index = 0;

    private void AppBarButton_Click(object sender, RoutedEventArgs e)
    {
        index++;
        string ButtonName = "Button" + index;
        Button dummyButton = new Button
        {
            Name = ButtonName,
            Content = ButtonName,
        };
        StackPanel1.Children.Add(dummyButton);
        dummyButton.RightTapped += new RightTappedEventHandler(DummyButton_RightTapped);
    }

    private void Button0_RightTapped(object sender, RightTappedRoutedEventArgs e)
    {
        MenuFlyout myFlyout = new MenuFlyout();
        MenuFlyoutItem firstItem = new MenuFlyoutItem { Text = "Right Clicked" };

        myFlyout.Items.Add(firstItem);
        myFlyout.ShowAt(sender as FrameworkElement);
    }

    private void DummyButton_RightTapped(object sender, RightTappedRoutedEventArgs e)
    {
        //var dialog = new MessageDialog("Right clicked");
        //await dialog.ShowAsync();

        MenuFlyout myFlyout = new MenuFlyout();
        MenuFlyoutItem firstItem = new MenuFlyoutItem { Text = "Right Clicked" };

        myFlyout.Items.Add(firstItem);
        myFlyout.ShowAt(sender as FrameworkElement);
    }
}

这是我的 XAML 代码:

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

<Page.TopAppBar >
    <AppBar IsSticky="True" IsRightTapEnabled="False" >
        <StackPanel HorizontalAlignment="Right" Orientation="Horizontal">
            <AppBarButton Label="Add Sound" Icon="OpenFile" Click="AppBarButton_Click" ></AppBarButton>
        </StackPanel>
    </AppBar>
</Page.TopAppBar>

<Grid Background="#FF004D40" Name="myGrid" IsRightTapEnabled="False">
    <Grid.RowDefinitions>
        <RowDefinition Height="1*"/>
        <RowDefinition Height="1*"/>
        <RowDefinition Height="1*"/>
        <RowDefinition Height="1*"/>
        <RowDefinition Height="1*"/>
        <RowDefinition Height="1*"/>
    </Grid.RowDefinitions>
    <StackPanel Name="StackPanel1" Grid.Row="0" Orientation="Horizontal" IsRightTapEnabled="False">
        <Button Content="Button0" Name ="Button0" RightTapped="Button0_RightTapped"></Button>
    </StackPanel>
</Grid>

【问题讨论】:

    标签: c# windows xaml uwp


    【解决方案1】:

    官方不建议在 UWP 中使用AppBar。以下部分参考官方AppBar说明。

    只有在升级使用 AppBar 的通用 Windows 8 应用程序并且需要最小化更改时才应使用AppBar。对于 Windows 10 中的新应用,我们建议改用 CommandBar 控件。

    用法

    CommandBar 添加到&lt;Page.TopAppBar&gt;,就像下面的代码一样。

     <Page.TopAppBar>
            <CommandBar IsSticky="True">
                <AppBarButton
                    Click="AppBarButton_Click"
                    Icon="OpenFile"
                    Label="Add Sound" />              
            </CommandBar>
        </Page.TopAppBar>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多