我希望程序在用户按住控件(在移动设备上)或当用户右键单击控件(在PC上)时显示附加的弹出框。
这是我的XAML:<DataTemplate x:DataType="data:Cards" x:Key="card">
<StackPanel x:Name="cardstack" Holding="cardstack_Holding" KeyDown="cardstack_KeyDown" >
<StackPanel Background="Blue" Height="100" />
<FlyoutBase.AttachedFlyout>
<MenuFlyout x:Name="optionpass">
<MenuFlyoutItem x:Name="delete" Text="Delete" Click="delete_Click"/>
</MenuFlyout>
</FlyoutBase.AttachedFlyout>
</StackPanel>
</DataTemplate>
这是我的c#:
private void cardstack_Holding(object sender, HoldingRoutedEventArgs e)
{
FlyoutBase.ShowAttachedFlyout(sender as FrameworkElement);
}
private void cardstack_KeyDown(object sender, KeyRoutedEventArgs e)
{
if (e.Key == Windows.System.VirtualKey.RightButton)
{
FlyoutBase.ShowAttachedFlyout(sender as FrameworkElement);
}
}
当我点击并按住移动模拟器上的Stackpanel时,按住事件有效,但当我右键单击PC时,它崩溃了!上面写着"没有附加的Flyout!"我不知道哪里不对。
"你试过右击事件了吗? "它有效吗?"
是和否:(
我刚刚找到了解决问题的方法。
原来你必须命名MenuFlyout
像我的一个是x:Name = "option_menu"
,而Flyoutbase.AttachedFlyout
不能在DataTemplate
,意味着你必须把它放在其他地方除了DataTemplate
,所以。cs文件可以找到MenuFlyout
的名字。
这是我的c#:
public void cardstack_Holding(object sender, HoldingRoutedEventArgs e)
{
option_menu.ShowAt(sender as FrameworkElement);
e.Handled = true;
}
private void cardstack_PointerPressed(object sender, PointerRoutedEventArgs e)
{
Pointer pointr = e.Pointer;
if (pointr.PointerDeviceType == Windows.Devices.Input.PointerDeviceType.Mouse)
{
Windows.UI.Input.PointerPoint pointrd = e.GetCurrentPoint(sender as UIElement);
if (pointrd.Properties.IsRightButtonPressed)
{
option_menu.ShowAt(sender as FrameworkElement);
}
}
e.Handled = true;
}
注意,在此之前我使用ShowAttachedFlyout
,现在我使用option_menu.ShowAt
。
KeyDown
事件不知何故不能与我的应用程序工作,所以我使用PointerPressed
代替。
希望这对你有帮助。(0 w0)/