How to create contextMenu in UWP App

  1. // MainPage1.cs  
  2. using System;  
  3. using System.Collections.Generic;  
  4. using Windows.UI.Xaml.Controls;  
  5. namespace SDKTemplate  
  6. {  
  7. public partial class MainPage : Page  
  8. {  
  9. public const string FEATURE_NAME = "Context menu C# sample";  
  10. List<Scenario> scenarios = new List<Scenario>  
  11. {  
  12. new Scenario() { Title = "Show a context menu", ClassType = typeof(Scenario1) },  
  13. new Scenario() { Title = "Replace a default context menu", ClassType = typeof(Scenario2) },  
  14. };  
  15. }  
  16. public class Scenario  
  17. {  
  18. public string Title { getset; }  
  19. public Type ClassType { getset; }  
  20. }  
  21. }  
  22. // Scenario1.cs  
  23. using System;  
  24. using Windows.Foundation;  
  25. using Windows.UI.Popups;  
  26. using Windows.UI.Xaml;  
  27. using Windows.UI.Xaml.Controls;  
  28. using Windows.UI.Xaml.Input;  
  29. using Windows.UI.Xaml.Media;  
  30. using Windows.UI.Xaml.Navigation;  
  31. namespace SDKTemplate  
  32. {  
  33. public sealed partial class Scenario1 : Page  
  34. {  
  35. public Scenario1()  
  36. {  
  37. this.InitializeComponent();  
  38. }  
  39. public static Rect GetElementRect(FrameworkElement element)  
  40. {  
  41. GeneralTransform buttonTransform = element.TransformToVisual(null);  
  42. Point point = buttonTransform.TransformPoint(new Point());  
  43. return new Rect(point, new Size(element.ActualWidth, element.ActualHeight));  
  44. }  
  45. private async void AttachmentImage_RightTapped(object sender, RightTappedRoutedEventArgs e)  
  46. {  
  47. var menu = new PopupMenu();  
  48. menu.Commands.Add(new UICommand("Open with", (command) =>  
  49. {  
  50. OutputTextBlock.Text = "'" + command.Label + "' selected";  
  51. }));  
  52. menu.Commands.Add(new UICommand("Save attachment", (command) =>  
  53. {  
  54. OutputTextBlock.Text = "'" + command.Label + "' selected";  
  55. }));  
  56. OutputTextBlock.Text = "Context menu shown";  
  57. var chosenCommand = await menu.ShowForSelectionAsync(GetElementRect((FrameworkElement)sender));  
  58. if (chosenCommand == null// The command is null if no command was invoked.  
  59. {  
  60. OutputTextBlock.Text = "Context menu dismissed";  
  61. }  
  62. }  
  63. }  
  64. }  
  65. //scenario2.cs  
  66. using System;  
  67. using Windows.ApplicationModel.DataTransfer;  
  68. using Windows.Foundation;  
  69. using Windows.System;  
  70. using Windows.UI.Popups;  
  71. using Windows.UI.Xaml;  
  72. using Windows.UI.Xaml.Controls;  
  73. using Windows.UI.Xaml.Navigation;  
  74. using Windows.UI.Xaml.Media;  
  75. namespace SDKTemplate  
  76. {  
  77. public sealed partial class Scenario2 : Page  
  78. {  
  79. public Scenario2()  
  80. {  
  81. this.InitializeComponent();  
  82. }  
  83. private Rect GetTextboxSelectionRect(TextBox textbox)  
  84. {  
  85. Rect rectFirst, rectLast;  
  86. if (textbox.SelectionStart == textbox.Text.Length)  
  87. {  
  88. rectFirst = textbox.GetRectFromCharacterIndex(textbox.SelectionStart - 1, true);  
  89. }  
  90. else  
  91. {  
  92. rectFirst = textbox.GetRectFromCharacterIndex(textbox.SelectionStart, false);  
  93. }  
  94. int lastIndex = textbox.SelectionStart + textbox.SelectionLength;  
  95. if (lastIndex == textbox.Text.Length)  
  96. {  
  97. rectLast = textbox.GetRectFromCharacterIndex(lastIndex - 1, true);  
  98. }  
  99. else  
  100. {  
  101. rectLast = textbox.GetRectFromCharacterIndex(lastIndex, false);  
  102. }  
  103. GeneralTransform buttonTransform = textbox.TransformToVisual(null);  
  104. Point point = buttonTransform.TransformPoint(new Point());  
  105. double x, y, dx, dy;  
  106. y = point.Y + rectFirst.Top;  
  107. dy = rectLast.Bottom - rectFirst.Top;  
  108. if (rectLast.Right > rectFirst.Left)  
  109. {  
  110. x = point.X + rectFirst.Left;  
  111. dx = rectLast.Right - rectFirst.Left;  
  112. }  
  113. else  
  114. {  
  115. x = point.X + rectLast.Right;  
  116. dx = rectFirst.Left - rectLast.Right;  
  117. }  
  118. return new Rect(x, dx, y, dy);  
  119. }  
  120. private async void ReadOnlyTextBox_ContextMenuOpening(object sender, ContextMenuEventArgs e)  
  121. {  
  122. e.Handled = true;  
  123. TextBox textbox = (TextBox)sender;  
  124. if (textbox.SelectionLength > 0)  
  125. {  
  126. var menu = new PopupMenu();  
  127. menu.Commands.Add(new UICommand("Copy"null, 1));  
  128. menu.Commands.Add(new UICommandSeparator());  
  129. menu.Commands.Add(new UICommand("Highlight"null, 2));  
  130. menu.Commands.Add(new UICommand("Look up"null, 3));  
  131. OutputTextBlock.Text = "Context menu shown";  
  132. Rect rect = GetTextboxSelectionRect(textbox);  
  133. var chosenCommand = await menu.ShowForSelectionAsync(rect);  
  134. if (chosenCommand != null)  
  135. {  
  136. switch ((int)chosenCommand.Id)  
  137. {  
  138. case 1:  
  139. String selectedText = ((TextBox)sender).SelectedText;  
  140. var dataPackage = new DataPackage();  
  141. dataPackage.SetText(selectedText);  
  142. Clipboard.SetContent(dataPackage);  
  143. OutputTextBlock.Text = "'" + chosenCommand.Label + "'(" + chosenCommand.Id.ToString() + ") selected; '" + selectedText + "' copied to clipboard";  
  144. break;  
  145. case 2:  
  146. OutputTextBlock.Text = "'" + chosenCommand.Label + "'(" + chosenCommand.Id.ToString() + ") selected";  
  147. break;  
  148. case 3:  
  149. OutputTextBlock.Text = "'" + chosenCommand.Label + "'(" + chosenCommand.Id.ToString() + ") selected";  
  150. break;  
  151. }  
  152. }  
  153. else  
  154. {  
  155. OutputTextBlock.Text = "Context menu dismissed";  
  156. }  
  157. }  
  158. else  
  159. {  
  160. OutputTextBlock.Text = "Context menu not shown because there is no text selected";  
  161. }  
  162. }  
  163. }  
  164. }