UIElement.ContextCanceled 事件

定义

当上下文输入手势继续转换为操作手势时发生,以通知元素不应打开上下文浮出控件。

// Register
event_token ContextCanceled(TypedEventHandler<UIElement, RoutedEventArgs const&> const& handler) const;

// Revoke with event_token
void ContextCanceled(event_token const* cookie) const;

// Revoke with event_revoker
UIElement::ContextCanceled_revoker ContextCanceled(auto_revoke_t, TypedEventHandler<UIElement, RoutedEventArgs const&> const& handler) const;
public event TypedEventHandler<UIElement,RoutedEventArgs> ContextCanceled;
function onContextCanceled(eventArgs) { /* Your code */ }
uIElement.addEventListener("contextcanceled", onContextCanceled);
uIElement.removeEventListener("contextcanceled", onContextCanceled);
- or -
uIElement.oncontextcanceled = onContextCanceled;
Public Custom Event ContextCanceled As TypedEventHandler(Of UIElement, RoutedEventArgs) 
<uiElement ContextCanceled="eventhandler"/>

事件类型

Windows 要求

设备系列
Windows 10 Anniversary Edition (在 10.0.14393.0 中引入)
API contract
Windows.Foundation.UniversalApiContract (在 v3.0 中引入)

示例

此示例演示如何在用户右键单击或执行等效操作时显示和隐藏上下文菜单。 上下文菜单提供“红色”和“绿色”选项,并放置在矩形上。

显示红色和绿色选项的上下文菜单
<Page
    ...>
    <Page.Resources>
        <MenuFlyout x:Key="colorMenuFlyout">
            <MenuFlyoutItem Text="Red" Tag="red" Click="MenuFlyoutItem_Click"/>
            <MenuFlyoutItem Text="Green" Tag="green" Click="MenuFlyoutItem_Click"/>
        </MenuFlyout>
    </Page.Resources>

    <Grid>
        <Rectangle Width="100" Height="100" Fill="Yellow"
                   ContextRequested="Color_ContextRequested" 
                   ContextCanceled="Color_ContextCanceled">
        </Rectangle>
    </Grid>
</Page>
public sealed partial class MainPage : Page
{
    MenuFlyout colorMenuFlyout;

    public MainPage()
    {
        this.InitializeComponent();

        colorMenuFlyout = Resources["colorMenuFlyout"] as MenuFlyout;
    }

    private void Color_ContextRequested(UIElement sender, ContextRequestedEventArgs args)
    {
        Point point = new Point(0,0);

        if (args.TryGetPosition(sender, out point))
        {
            colorMenuFlyout.ShowAt(sender, point);
        }
        else
        {
            colorMenuFlyout.ShowAt((FrameworkElement)sender);
        }
    }

    private void Color_ContextCanceled(UIElement sender, RoutedEventArgs args)
    {
        colorMenuFlyout.Hide();
    }

    private void MenuFlyoutItem_Click(object sender, RoutedEventArgs e)
    {
        var item = sender as MenuFlyoutItem;
        var target = colorMenuFlyout.Target;
        if (string.Equals(item.Tag.ToString(), "red"))
        {
            ((Rectangle)target).Fill = new SolidColorBrush(Windows.UI.Colors.Red);
        }
        else if (string.Equals(item.Tag.ToString(), "green"))
        {
            ((Rectangle)target).Fill = new SolidColorBrush(Windows.UI.Colors.Green);
        }
    }
}

注解

建议设置 ContextFlyout 属性,以便向元素添加上下文菜单。 设置 时 ContextFlyout ,上下文菜单会自动显示和隐藏。 如果未设置 ContextFlyout,则只应处理 ContextRequestedContextCanceled

如果处理 ContextRequested 事件以显示上下文浮出控件,则还应处理此事件以在请求被取消时隐藏浮出控件。

通常,对于可以通过拖放操作的元素,你通常会处理此事件。 当 已引发 ContextRequested 事件,但在操作开始之前元素尚未收到 PointerReleased 事件时,将引发此事件。 这表示用户打算调用操作而不是上下文浮出控件,因此不应打开上下文浮出控件。

ContextCanceled 是路由事件。 有关路由事件概念的详细信息,请参阅 事件和路由事件概述

适用于

另请参阅