Metro style App ContextMenu Summary

简介:

 

Metro style App ContextMenu Summary。

Fist let us see the effect pictures。

Picture 1.

Picture 2.

 

Get the frameworkElement Rect。

public  static  Rect GetElementRect(FrameworkElement element)
{
     GeneralTransform buttonTransform = element.TransformToVisual( null );
     Point point = buttonTransform.TransformPoint( new  Point());
     return  new  Rect(point, new  Size(element.ActualWidth, element.ActualHeight));
}

 

Next is a image righttap event。

private  async void  AttachmentImage_RightTapped( object  sender, RightTappedRoutedEventArgs e)
{
     var  menu = new  PopupMenu();
     menu.Commands.Add( new  UICommand( "Open with" , (command) =>
     {
         //ToDo:function
     }));
     menu.Commands.Add( new  UICommand( "Save attachment" , (command) =>
     {
         //ToDo:function
     }));
 
     var  chosenCommand = await menu.ShowForSelectionAsync(GetElementRect((FrameworkElement)sender));
     if  (chosenCommand == null )
     {
         // The command is null if no command was invoked.
         //ToDo:function
     }
}

 

 

 2.Another sample

 returns a rect for selected text

// returns a rect for selected text
private  Rect GetTextboxSelectionRect(TextBox textbox)
{
     Rect rectFirst, rectLast;
     if  (textbox.SelectionStart == textbox.Text.Length)
     {
         rectFirst = textbox.GetRectFromCharacterIndex(textbox.SelectionStart - 1, true );
     }
     else
     {
         rectFirst = textbox.GetRectFromCharacterIndex(textbox.SelectionStart, false );
     }
 
     int  lastIndex = textbox.SelectionStart + textbox.SelectionLength;
     if  (lastIndex == textbox.Text.Length)
     {
         rectLast = textbox.GetRectFromCharacterIndex(lastIndex - 1, true );
     }
     else
     {
         rectLast = textbox.GetRectFromCharacterIndex(lastIndex, false );
     }
 
     GeneralTransform buttonTransform = textbox.TransformToVisual( null );
     Point point = buttonTransform.TransformPoint( new  Point());
 
     return  new  Rect(point.X + rectFirst.Left,
         point.Y + rectFirst.Top,
         rectLast.Right - rectFirst.Left,
         rectLast.Bottom - rectFirst.Top);
}

 

Next is a TextBox ContextMenuOpening event.

private  async void  ReadOnlyTextBox_ContextMenuOpening( object  sender, ContextMenuEventArgs e)
{
     e.Handled = true ;
     TextBox textbox = (TextBox)sender;
     if  (textbox.SelectionLength > 0)
     {
         var  menu = new  PopupMenu();
         menu.Commands.Add( new  UICommand( "Copy" , null , 1));
         menu.Commands.Add( new  UICommandSeparator());
         menu.Commands.Add( new  UICommand( "Highlight" , null , 2));
         menu.Commands.Add( new  UICommand( "Look up" , null , 3));
 
         Rect rect = GetTextboxSelectionRect(textbox);
         var  chosenCommand = await menu.ShowForSelectionAsync(rect);
         if  (chosenCommand != null )
         {
             switch  (( int )chosenCommand.Id)
             {
                 case  1:
                     //Next is copy function
                     String selectedText = ((TextBox)sender).SelectedText;
                     var  dataPackage = new  DataPackage();
                     dataPackage.SetText(selectedText);
                     Clipboard.SetContent(dataPackage);
                     break ;
 
                 case  2:
                     //Todo:function
                     break ;
 
                 case  3:
                     //Todo:function
                     break ;
             }
         }
         else
         {
             //Todo:function
         }
     }
     else
     {
         //Todo:function
     }
}

 

The source sample is from msdn.



本文转自Work Hard Work Smart博客园博客,原文链接:http://www.cnblogs.com/linlf03/archive/2012/08/01/2618865.html,如需转载请自行联系原作者

目录
相关文章
|
2月前
|
小程序 JavaScript 前端开发
uni-app开发微信小程序:四大解决方案,轻松应对主包与vendor.js过大打包难题
uni-app开发微信小程序:四大解决方案,轻松应对主包与vendor.js过大打包难题
758 1
|
3天前
|
JSON 缓存 前端开发
HarmonyOS NEXT 5.0鸿蒙开发一套影院APP(附带源码)
本项目基于HarmonyOS NEXT 5.0开发了一款影院应用程序,主要实现了电影和影院信息的展示功能。应用包括首页、电影列表、影院列表等模块。首页包含轮播图与正在热映及即将上映的电影切换显示;电影列表模块通过API获取电影数据并以网格形式展示,用户可以查看电影详情;影院列表则允许用户选择城市后查看对应影院信息,并支持城市选择弹窗。此外,项目中还集成了Axios用于网络请求,并进行了二次封装以简化接口调用流程,同时添加了请求和响应拦截器来处理通用逻辑。整体代码结构清晰,使用了组件化开发方式,便于维护和扩展。 该简介概括了提供的内容,但请注意实际开发中还需考虑UI优化、性能提升等方面的工作。
41 11

热门文章

最新文章