win10 uwp 拖动控件
来源:互联网 发布:yy头像制作软件 编辑:程序博客网 时间:2024/06/07 16:24
我们会使用控件拖动,可以让我们做出好看的动画,那么我们如何移动控件,我将会告诉大家多个方法。其中第一个是最差的,最后的才是我希望大神你去用。
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 ),不得用于商业目的,基于本文修改后的作品务必以相同的许可发布。如有任何疑问,请与我联系。
- win10 uwp 拖动控件
- win10 uwp 自定义控件 SplitViewItem
- win10 uwp 自定义控件初始化
- win10开发中 UWP SplitView 控件讲解
- win10 uwp 随着数字变化颜色控件
- win10 uwp InkCanvas控件数据绑定
- win10 uwp 绘图 Line 控件使用
- win10 uwp 验证输入 自定义用户控件
- win10 uwp 获得Slider拖动结束的值
- uwp开发:Slider控件和MediaElement绑定,实现拖动播放.
- Win10开发之UWP控件的隐藏空间
- win10 uwp 如何拖动一个TextBlock的文字到另一个TextBlock
- win10 UWP读写文件
- Win10/UWP开发-Ink
- win10 uwp 通知Toast
- win10 UWP 显示地图
- win10 uwp 参考
- win10 uwp clone
- BZOJ 1179: [Apio2009]Atm
- (c语言)结构体方法,计算该日是当年的第几天
- 建立HashSet的实例,依次填入三个名字判断实例中是否存在"Kobe"
- C++中的类继承(1) 三种继承方式
- Windows 10 很快变得更安全
- win10 uwp 拖动控件
- vim中文帮助文档安装
- next_permutation函数(全排列)
- 视觉导航路径编辑器使用教程
- 欧拉函数模板
- EA&UML日拱一卒--类图::泛化(generalization)
- poj 1469 COURSES
- Could not find method update(View) in a parent or ancestor Context for android:onClick attribute def
- 【NOI2015模拟9.9】文理分科