• win10 uwp 拖动控件


    我们会使用控件拖动,可以让我们做出好看的动画,那么我们如何移动控件,我将会告诉大家多个方法。其中第一个是最差的,最后的才是我希望大神你去用。

    Margin 移动

    我们可以使用Margin移动,但这是wr说不要这样做。

    We can move the control by Margin,but using this method is not recommended.

    我们可以在xaml写一个Button,然后就使用左键获取鼠标,这个可以去看 win10 uwp 获取按钮鼠标左键按下

    http://lindexi.oschina.io/lindexi/post/win10-uwp-%E8%8E%B7%E5%8F%96%E6%8C%89%E9%92%AE%E9%BC%A0%E6%A0%87%E5%B7%A6%E9%94%AE%E6%8C%89%E4%B8%8B/

    于是在Button_OnPointerMoved,我们获取移动的xy

    PointerPoint point = e.GetCurrentPoint(btn);
    

    这样point.Position.X就是移动的左边

    我们可以通过x += point.Position.X - btn.ActualWidth / 2.0;

    这是因为btn.ActualWidth / 2.0不用的话会是控件的左上角。

    我们把它给Margin

            private void Button_OnPointerMoved(object sender, PointerRoutedEventArgs e)
            {
                Button btn=sender as Button;
                if (btn == null)
                {
                    return;
                }
                e.Handled = true;
    
                PointerPoint point = e.GetCurrentPoint(btn);
    
                if (point.Properties.IsLeftButtonPressed)
                {
                    double x = (double)btn.GetValue(Canvas.LeftProperty);
                    double y = (double)btn.GetValue(Canvas.TopProperty);
                    x += point.Position.X - btn.ActualWidth / 2.0;
                    y += point.Position.Y - btn.ActualHeight / 2.0;
                    btn.Margin=new Thickness(x,y,0,0);
                }
            }
    

    Canvas 拖动控件

    我们需要把控件放在Canvas,然后使用Margin一样的

    我们需要设置附件属性,btn.SetValue(Canvas.LeftProperty, x)就是设置Canvas.Left

            private void Button_OnPointerMoved(object sender, PointerRoutedEventArgs e)
            {
                Button btn=sender as Button;
                if (btn == null)
                {
                    return;
                }
                e.Handled = true;
    
                PointerPoint point = e.GetCurrentPoint(btn);
    
                if (point.Properties.IsLeftButtonPressed)
                {
                    double x = (double)btn.GetValue(Canvas.LeftProperty);
                    double y = (double)btn.GetValue(Canvas.TopProperty);
                    x += point.Position.X - btn.ActualWidth / 2.0;
                    y += point.Position.Y - btn.ActualHeight / 2.0;
                    btn.SetValue(Canvas.LeftProperty, x);
                    btn.SetValue(Canvas.TopProperty, y);
                }
            }
    

    Manipulation 拖动控件

    我们可以使用手势,这个需要在控件设置ManipulationMode="All",使用ManipulationDelta

            private void Button_OnManipulationDelta(object sender, ManipulationDeltaRoutedEventArgs e)
            {
                Button btn = sender as Button;
                if (btn == null)
                {
                    return;
                }
    
                if (dragTranslation == null)
                {
                    dragTranslation = new TranslateTransform();
                }
    
                btn.RenderTransform = dragTranslation;
    
                dragTranslation.X += e.Delta.Translation.X;
                dragTranslation.Y += e.Delta.Translation.Y;
            }
    

    做好之后,我们发现实在奇怪

    大神,请用力划。
    
    大神:我的控件哪去?
    
    控件:谁叫你那么用力
    
    Canvas:我的左边可以长度无限。
    
    ……
    

    好在OneWindows的帮助

    参见:http://www.cnblogs.com/cjw1115/p/5323339.html

    知识共享许可协议
    本作品采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可。欢迎转载、使用、重新发布,但务必保留文章署名林德熙(包含链接:http://blog.csdn.net/lindexi_gd ),不得用于商业目的,基于本文修改后的作品务必以相同的许可发布。如有任何疑问,请与我联系

  • 相关阅读:
    13.1 CentOS系统启动流程介绍
    MSSS攝影大賽計劃書(第三版)
    vuex-cart 介绍
    Golang Slice 总结
    使用jQuery在屏幕上居中一个DIV
    多线程-阻塞队列
    javascript常用知识汇总
    CocoaPods 安装和使用
    吴裕雄 12-MySQL WHERE 子句
    吴裕雄 11-MySQL查询数据
  • 原文地址:https://www.cnblogs.com/lindexi/p/12087416.html
Copyright © 2020-2023  润新知