WinRT中实现元素拖拉效果

前面有一篇博客涉及到手势和指针的用法,其中就有利用手势进行元素的拖拉操作,主要是ManipulationDelta:

博客地址: Windows Phone 8.1触控输入-----手势及指针

其实利用手势ManipulationDelta操作来实现元素拖拉的方法很简单,很实用。但是一旦要实现页面中多个控件元素

的拖拉,难免代码臃肿。其实我们可以抽象出这其中一致的功能代码,这样要实现某一个元素的拖拉效果只需调用共

通类中的共有函数即可。


所以下面介绍的是如何封装成共有的类来进行调用:

(这里主要是利用PointerPressed,PointerMoved,PointerReleased三个方法)

1.首先是XAML代码,页面布局只要放一个canvas,再在canvas下放一个rectangle

<Page
    x:Class="DragDemo.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:DragDemo"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

    <Grid>
        <Canvas Width="300" Height="300" Background="White">
            <Rectangle x:Name="rect" Width="100" Height="100" Fill="Coral"/>
        </Canvas>
    </Grid>
</Page>

2..CS代码:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Popups;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// “空白页”项模板在 http://go.microsoft.com/fwlink/?LinkId=391641 上有介绍

namespace DragDemo
{
    /// <summary>
    /// 可用于自身或导航至 Frame 内部的空白页。
    /// </summary>
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();

            this.NavigationCacheMode = NavigationCacheMode.Required;

            this.Loaded += MainPage_Loaded;
        }

        void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            DragHelper.Dragable(rect);
        }

        /// <summary>
        /// 在此页将要在 Frame 中显示时进行调用。
        /// </summary>
        /// <param name="e">描述如何访问此页的事件数据。
        /// 此参数通常用于配置页。</param>
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            // TODO: 准备此处显示的页面。

            // TODO: 如果您的应用程序包含多个页面,请确保
            // 通过注册以下事件来处理硬件“后退”按钮:
            // Windows.Phone.UI.Input.HardwareButtons.BackPressed 事件。
            // 如果使用由某些模板提供的 NavigationHelper,
            // 则系统会为您处理该事件。
        }

        
    }
}

3.共通类的共通函数:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.Foundation;
using Windows.UI.Popups;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;

namespace DragDemo
{
    public static class DragHelper
    {
        public static readonly DependencyProperty IsDraggingProperty = DependencyProperty.RegisterAttached(
            "IsDragging", typeof(bool), typeof(DragHelper), new PropertyMetadata(false));

        public static readonly DependencyProperty StartLeftProperty = DependencyProperty.RegisterAttached("StartLeft",
            typeof(double), typeof(DragHelper), new PropertyMetadata(0.0d));

        public static readonly DependencyProperty StartTopProperty = DependencyProperty.RegisterAttached("StartTop",
            typeof(double), typeof(DragHelper), new PropertyMetadata(0.0d));

        public static readonly DependencyProperty StartPositionProperty =
            DependencyProperty.RegisterAttached("StartPosition", typeof(Point), typeof(DragHelper),
                new PropertyMetadata(default(Point)));

        public static bool Dragable(this UIElement control)
        {
            if (control == null)
            {
                throw new ArgumentNullException("control");
            }
            if (VisualTreeHelper.GetParent(control) is Canvas)
            {
                control.PointerPressed += (sender, e) =>
                {
                    control.SetValue(IsDraggingProperty, true);
                    control.SetValue(StartLeftProperty, Canvas.GetLeft(control));
                    control.SetValue(StartTopProperty, Canvas.GetTop(control));
                    control.SetValue(StartPositionProperty, e.GetCurrentPoint(null).Position);
                };
                var coreWindow = Window.Current.CoreWindow;
                coreWindow.PointerMoved += (sender, args) =>
                {
                    if ((bool)control.GetValue(IsDraggingProperty))
                    {
                        var currentPosition = args.CurrentPoint.Position;
                        var startPosition = (Point)control.GetValue(StartPositionProperty);
                        var deltaX = currentPosition.X - startPosition.X;
                        var deltaY = currentPosition.Y - startPosition.Y;
                        var startLeft = (double)control.GetValue(StartLeftProperty);
                        var startTop = (double)control.GetValue(StartTopProperty);
                        Canvas.SetLeft(control, startLeft + deltaX);
                        Canvas.SetTop(control, startTop + deltaY);
                    }
                };
                coreWindow.PointerReleased += (sender, args) => control.SetValue(IsDraggingProperty, false);

                return true;
            }
            else
            {
                return false;
            }
        }
    }
}

推荐链接: 【WinRT】让控件飞,WinRT 中实现 web 中的 dragable 效果


上述过程中,我遇到一个问题,我直接在MainPage构造函数中直接调用共通函数,然后就会报错。后来我想因为在

其中使用到VisualTreeHelper.GetParent(UIElement...),通过可视化树操作获取需要拖拉控件的父元素,但是一旦

页面还未加载完成就去获取当然应该会出错。所以我之后在页面的load事件里才去调用共通函数,这下应该就不会出

错了。


1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 、4下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看rEADME.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值