zoukankan      html  css  js  c++  java
  • 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/
    欢迎任何形式的转载,未经作者同意,请保留此段声明!

  • 相关阅读:
    iOS 字典转模型 KVC 实现
    iOS开发 滤镜的使用
    iOS开发 二维码生成
    iOS开发 iOS10兼容访问http
    Jmockit 使用小计 1.46
    springboot样例 pom与小花招
    react + antd html网页配置非框架
    Vue 设置 vue router 路径错误检查
    java 8 读取配置文件例子
    mysql 项目配置
  • 原文地址:https://www.cnblogs.com/linlf03/p/2618865.html
Copyright © 2011-2022 走看看