桌面应用程序的保持事件未触发
Holding event for desktop app not firing
在我的 Universal Windows 应用程序 中,我订阅了我的 ListViewItem DataTemplate 中的 Holding 事件:
<ListView.ItemTemplate>
<DataTemplate>
<Grid Holding="ListViewItem_Holding">
<FlyoutBase.AttachedFlyout>
<MenuFlyout Placement="Right">
<!-- using the Click event -->
<MenuFlyoutItem Text="delete" Click="DeleteProductClick" />
<MenuFlyoutItem Text="edit" />
</MenuFlyout>
</FlyoutBase.AttachedFlyout>
<TextBlock Text="{Binding Name}" />
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
使用 Visual Studio 模拟器和触摸模式一切正常,但我找不到使用鼠标 调用上下文菜单的方法 。我是否必须使用 GestureRecognizer 才能使上下文菜单适用于桌面应用程序(相对于平板电脑版本)?
根据您将在 MSDN 找到的内容:
Holding for mouse and pen/stylus input
Mouse input doesn't produce Holding events by default, no matter how long a mouse button is held down, or which button is held. However, mouse devices and some pen devices can fire RightTapped when a right mouse button or equivalent is pressed and released.
Note There is a way to treat mouse actions as hold actions if you use your own GestureRecognizer and specify HoldWithMouse in settings.
在这种情况下,您可能必须使用自定义 GestureRecognizer 或使用 Pointer 事件。
我会改用 RightTapped 事件,以便当用户在基于触摸的设备中按住某个项目时以及当用户在基于鼠标的设备中用鼠标右键单击时显示上下文菜单:
<Page.Resources>
<MenuFlyout x:Key="flyout">
...
</MenuFlyout>
</Page.Resources>
...
<ListView ....>
<ListView.ItemTemplate>
<DataTemplate>
<Grid RightTapped="grid_RightTapped">
...
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
然后在后面的代码中处理事件:
private void grid_RightTapped(object sender, RightTappedRoutedEventArgs e) {
this.flyout.ShowAt(this, e.GetPosition(this));
e.Handled = true;
}
如你所见,我使用了共享弹出窗口,你可以按照自己的方式进行。
在我的 Universal Windows 应用程序 中,我订阅了我的 ListViewItem DataTemplate 中的 Holding 事件:
<ListView.ItemTemplate>
<DataTemplate>
<Grid Holding="ListViewItem_Holding">
<FlyoutBase.AttachedFlyout>
<MenuFlyout Placement="Right">
<!-- using the Click event -->
<MenuFlyoutItem Text="delete" Click="DeleteProductClick" />
<MenuFlyoutItem Text="edit" />
</MenuFlyout>
</FlyoutBase.AttachedFlyout>
<TextBlock Text="{Binding Name}" />
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
使用 Visual Studio 模拟器和触摸模式一切正常,但我找不到使用鼠标 调用上下文菜单的方法 。我是否必须使用 GestureRecognizer 才能使上下文菜单适用于桌面应用程序(相对于平板电脑版本)?
根据您将在 MSDN 找到的内容:
Holding for mouse and pen/stylus input
Mouse input doesn't produce Holding events by default, no matter how long a mouse button is held down, or which button is held. However, mouse devices and some pen devices can fire RightTapped when a right mouse button or equivalent is pressed and released.
Note There is a way to treat mouse actions as hold actions if you use your own GestureRecognizer and specify HoldWithMouse in settings.
在这种情况下,您可能必须使用自定义 GestureRecognizer 或使用 Pointer 事件。
我会改用 RightTapped 事件,以便当用户在基于触摸的设备中按住某个项目时以及当用户在基于鼠标的设备中用鼠标右键单击时显示上下文菜单:
<Page.Resources>
<MenuFlyout x:Key="flyout">
...
</MenuFlyout>
</Page.Resources>
...
<ListView ....>
<ListView.ItemTemplate>
<DataTemplate>
<Grid RightTapped="grid_RightTapped">
...
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
然后在后面的代码中处理事件:
private void grid_RightTapped(object sender, RightTappedRoutedEventArgs e) {
this.flyout.ShowAt(this, e.GetPosition(this));
e.Handled = true;
}
如你所见,我使用了共享弹出窗口,你可以按照自己的方式进行。