In Windows 8, controls include contextmenuopening events of input controls such as textbox and righttapped events of non-input controls such as buttons.
This article describes the usage of two events. The
Popupmenu is a class of the shortcut menu.
First, let's take a look at the instantiation of the specific menu class and get the project code.
/// <Summary> /// right-click and choose processing details /// </Summary> /// <Param name = "sender"> </param> Public async void setrightclick (Object sender) {// Add menu popupmenu menu = new popupmenu (); menu. commands. add (New uicommand ("Copy 0", null, 0); menu. commands. add (New uicommand ("Cut 1", null, 1); menu. commands. add (New uicommand ("paste 2", null, 2); menu. commands. add (New uicommandseparator (); menu. commands. add (New uicommand ("F Ull screen ", null, 3); menu. commands. add (New uicommand ("snap screen", null, 4); // obtain the selected menu item var cmd = await menu. showforselectionasync (getrectposition (frameworkelement) sender); If (CMD! = NULL) {Switch (INT) cmd. ID) {Case 0: tbtext. TEXT = "selected (" + cmd. label + "), whose ID is" + cmd. ID; break; Case 1: tbtext. TEXT = "selected (" + cmd. label + "), whose ID is" + cmd. ID; break; Case 2: tbtext. TEXT = "selected (" + cmd. label + "), whose ID is" + cmd. ID; break; Case 3: tbtext. TEXT = "selected (" + cmd. label + "), whose ID is" + cmd. ID; break; Case 4: tbtext. TEXT = "selected (" + cmd. label + "), whose ID is" + cmd. ID; break ;}} else {tbtext. TEXT = "context menu" ;}}// obtain the menu position rect getrectposition (frameworkelement element) {generaltransform btnform = element. transformtovisual (null); point = btnform. transformpoint (new point (); return New rect (point, new size (element. actualwidth, element. actualheight ));}
Then let's take a look at the event of registering the right-click processing of the two controls as follows:
/// <Summary> /// invoked when this page is about to be displayed in a frame. /// </Summary> /// <Param name = "E"> event data that describes how this page was reached. the parameter // property is typically used to configure the page. </param> protected override void onnavigatedto (navigationeventargs e) {base. onnavigatedto (E); // context menu this. tbname. contextmenuopening + = tbname_contextmenuopening; // right-click the menu this. gdmenu. righttapped + = gdmenu_righttapped ;} /// <summary> /// right-click the menu event /// </Summary> /// <Param name = "sender"> </param> /// <Param name = "E"> </param> void gdmenu_righttapped (Object sender, righttappedroutedeventargs e) {setrightclick (sender );} /// <summary> /// context menu /// </Summary> /// <Param name = "sender"> </param> /// <Param name = "E"> </param> void tbname_contextmenuopening (Object sender, contextmenueventargs e) {setrightclick (sender );} /// <summary> /// event uninstall /// </Summary> /// <Param name = "E"> </param> protected override void onnavigatedfrom (navigationeventargs E) {base. onnavigatedfrom (E); // context menu this. tbname. contextmenuopening-= tbname_contextmenuopening; // right-click the menu this. gdmenu. righttapped-= gdmenu_righttapped ;}
The front-end XAML code is as follows:
<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}" Name="gdMenu"> <TextBox HorizontalAlignment="Left" Margin="141,164,0,0" TextWrapping="Wrap" Name="tbName" Text="" VerticalAlignment="Top" Height="157" Width="540"/> <TextBox HorizontalAlignment="Left" Margin="210,353,0,0" TextWrapping="Wrap" Name="tbText" Text="" VerticalAlignment="Top" Width="290"/> </Grid>
To download the source code, click win8menu.rar.