How to Open Context Menu in Windows Phone 8

Let us see the procedure for opening a Context Menu in Windows Phone 8. Create a new project with a valid name, once everything is ready our project looks as in the following screen.

Windows phone 8

Now add one TextBlock control to the triggers and show the context menu, change the TexxtBlock content to “Open Context Menu”.

Next add a reference for Microsoft.Phone.Controls.Toolkit.dll to your project. You can get this DLL in the following URL.

Now add the following namespace in the XAML page.

xmlns:toolkit="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit"

XAML Code

  1. <phone:PhoneApplicationPage  
  2.     x:Class="ContextMenuDemo.MainPage"  
  3.     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
  4.     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
  5.     xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"  
  6.     xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"  
  7.     xmlns:d="http://schemas.microsoft.com/expression/blend/2008"  
  8.     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"  
  9.     xmlns:toolkit="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit"  
  10.     xmlns:c4fToolkit="clr-namespace:Coding4Fun.Phone.Controls;assembly=Coding4Fun.Phone.Controls"  
  11.     mc:Ignorable="d"  
  12.     FontFamily="{StaticResource PhoneFontFamilyNormal}"  
  13.     FontSize="{StaticResource PhoneFontSizeNormal}"  
  14.     Foreground="{StaticResource PhoneForegroundBrush}"  
  15.     SupportedOrientations="Portrait" Orientation="Portrait"  
  16.     shell:SystemTray.IsVisible="True">  
  17.       
  18.     <Grid x:Name="LayoutRoot" Background="Transparent">  
  19.         <Grid.RowDefinitions>  
  20.             <RowDefinition Height="Auto"/>  
  21.             <RowDefinition Height="*"/>  
  22.         </Grid.RowDefinitions>  
  23.         <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">  
  24.             <TextBlock Text="MY APPLICATION" Style="{StaticResource PhoneTextNormalStyle}" Margin="12,0"/>  
  25.         </StackPanel>  
  26.         <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">  
  27.             <TextBlock TextWrapping="Wrap" Text="Open Context Menu" Margin="98,218,129,441" FontSize="24">  
  28.                 <toolkit:ContextMenuService.ContextMenu>  
  29.                 <toolkit:ContextMenu Name="ctxMenuRoster">  
  30.                     <toolkit:MenuItem Header="option1" Click="MenuItem_Click"/>  
  31.                     <toolkit:MenuItem Header="option2" Click="MenuItem_Click_1"></toolkit:MenuItem>  
  32.                 </toolkit:ContextMenu>  
  33.             </toolkit:ContextMenuService.ContextMenu>  
  34.             </TextBlock>  
  35.         </Grid>  
  36.     </Grid>  
  37. </phone:PhoneApplicationPage>  
The following is the sample code snippet to implement a context menu in Windows Phone 8.

xml code

Here, I implemented two menu options, option1 and option2. This menu will open when we long press the TextBlock.

Run the application by pressing the F5 Key and see the output as shown in the following. Using a long press of the “Open Context Menu” TextBlock we get the excepted output.

 

Open Context Menu


Similar Articles