win10 uwp 拖动控件

it2022-05-07  3

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

<!--more-->

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-获取按钮鼠标左键按下/

于是在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 ),不得用于商业目的,基于本文修改后的作品务必以相同的许可发布。如有任何疑问,请与我联系。

<script type="text/javascript"> $(function () { $('pre.prettyprint code').each(function () { var lines = $(this).text().split('\n').length; var $numbering = $('<ul/>').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i <= lines; i++) { $numbering.append($('<li/>').text(i)); }; $numbering.fadeIn(1700); }); }); </script>

转载于:https://www.cnblogs.com/lindexi/p/6949662.html

相关资源:垃圾分类数据集及代码

最新回复(0)