UIElement.ContextRequested Kejadian

Definisi

Terjadi ketika pengguna telah menyelesaikan gerakan input konteks, seperti klik kanan.

// Register
event_token ContextRequested(TypedEventHandler<UIElement, ContextRequestedEventArgs const&> const& handler) const;

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

// Revoke with event_revoker
UIElement::ContextRequested_revoker ContextRequested(auto_revoke_t, TypedEventHandler<UIElement, ContextRequestedEventArgs const&> const& handler) const;
public event TypedEventHandler<UIElement,ContextRequestedEventArgs> ContextRequested;
function onContextRequested(eventArgs) { /* Your code */ }
uIElement.addEventListener("contextrequested", onContextRequested);
uIElement.removeEventListener("contextrequested", onContextRequested);
- or -
uIElement.oncontextrequested = onContextRequested;
Public Custom Event ContextRequested As TypedEventHandler(Of UIElement, ContextRequestedEventArgs) 
<uiElement ContextRequested="eventhandler"/>

Jenis Acara

Persyaratan Windows

Rangkaian perangkat
Windows 10 Anniversary Edition (diperkenalkan dalam 10.0.14393.0)
API contract
Windows.Foundation.UniversalApiContract (diperkenalkan dalam v3.0)

Contoh

Contoh ini menunjukkan cara menampilkan dan menyembunyikan menu konteks saat pengguna mengklik kanan atau melakukan tindakan yang setara. Menu konteks menawarkan opsi Merah dan Hijau dan ditempatkan pada persegi panjang.

Menu konteks memperlihatkan opsi merah dan hijau
<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);
        }
    }
}

Keterangan

Kami menyarankan Anda mengatur properti ContextFlyout untuk menambahkan menu konteks ke elemen. Saat ContextFlyout diatur, menu konteks ditampilkan dan disembunyikan secara otomatis, dan peristiwa ini ditandai sebagai ditangani. Anda seharusnya hanya menangani ContextRequested dan ContextCanceled jika Anda tidak mengatur ContextFlyout.

Jika Anda menangani peristiwa ini untuk menampilkan flyout konteks, Anda juga harus menangani peristiwa ContextCanceled untuk menyembunyikan flyout jika permintaan dibatalkan.

ContextRequested adalah peristiwa yang dirutekan. Untuk informasi selengkapnya tentang konsep peristiwa yang dirutekan, lihat Gambaran umum peristiwa dan peristiwa yang dirutekan.

Berlaku untuk

Lihat juga