【发布时间】: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>
【问题讨论】: