背水一战 Windows 10 (73) - 控件(控件基类): UIElement - 拖放的基本应用, 手动开启 UIElement 的拖放操作

本文涉及的产品
数据可视化DataV,5个大屏 1个月
可视分析地图(DataV-Atlas),3 个项目,100M 存储空间
简介: 原文:背水一战 Windows 10 (73) - 控件(控件基类): UIElement - 拖放的基本应用, 手动开启 UIElement 的拖放操作[源码下载] 背水一战 Windows 10 (73) - 控件(控件基类): UIElement - 拖放的基本应用, 手动开启 UIEle...
原文: 背水一战 Windows 10 (73) - 控件(控件基类): UIElement - 拖放的基本应用, 手动开启 UIElement 的拖放操作

[源码下载]


背水一战 Windows 10 (73) - 控件(控件基类): UIElement - 拖放的基本应用, 手动开启 UIElement 的拖放操作



作者:webabcd


介绍
背水一战 Windows 10 之 控件(控件基类 - UIElement)

  • 拖放的基本应用
  • 手动开启 UIElement 的拖放操作



示例
1、演示 UIElement 的 drag & drop 的基本应用
Controls/BaseControl/UIElementDemo/DragDropDemo1.xaml

<Page
    x:Class="Windows10.Controls.BaseControl.UIElementDemo.DragDropDemo1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:Windows10.Controls.BaseControl.UIElementDemo"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <Grid Background="Transparent">
        <StackPanel Margin="5">

            <!--
                用于演示如何 drag 一个元素,并传递文本数据
            -->
            <Grid Name="dragGrid1" Background="Orange" Margin="5"
                  CanDrag="True"  
                  DragStarting="dragGrid1_DragStarting" 
                  DropCompleted="dragGrid1_DropCompleted">
                <TextBlock Name="sourceTextBlock" Text="i am webabcd" Margin="20" />
            </Grid>

            <!--
                用于演示如何 drag 一个元素,并传递图片数据
            -->
            <Grid Name="dragGrid2" Background="Orange" Margin="5"
                  CanDrag="True"  
                  DragStarting="dragGrid2_DragStarting" 
                  DropCompleted="dragGrid2_DropCompleted">
                <Image Name="sourceImage" Source="/Assets/hololens.jpg" Width="50" Height="50" Margin="20" />
            </Grid>

            <!--
                用于演示如何将一个可 drag 的元素 drop 到此,并获取传递过来的数据
            -->
            <Grid Name="dropGrid" Background="Blue" Margin="5"
                  AllowDrop="True"
                  Drop="dropGrid_Drop"
                  DragEnter="dropGrid_DragEnter"
                  DragOver="dropGrid_DragOver"
                  DragLeave="dropGrid_DragLeave">
                <Image Name="targetImage" Width="400" Height="300" Margin="20" />
                <TextBlock Name="targetTextBlock" TextWrapping="Wrap" MinHeight="300" Margin="20" />
            </Grid>

            <TextBlock Name="lblMsg" Margin="5" />

        </StackPanel>
    </Grid>
</Page>

Controls/BaseControl/UIElementDemo/DragDropDemo1.xaml.cs

/*
 * UIElement - UIElement(继承自 DependencyObject, 请参见 /Controls/BaseControl/DependencyObjectDemo/)
 *     CanDrag - 此 UIElement 是否可以 drag
 *     DragStarting - 可以 drag 的 UIElement 开始 drag 时触发的事件
 *     DropCompleted - 可以 drag 的 UIElement 完成 drop 后触发的事件
 *     
 *     AllowDrop - 此 UIElement 是否可以 drop
 *     Drop -  可以 drop 的 UIElement 在 drop 操作发生时触发的事件
 *     DragEnter - drag 操作进入可以 drop 的 UIElement 时触发的事件
 *     DragOver - drag 操作在可以 drop 的 UIElement 上移动时触发的事件
 *     DragLeave - drag 操作离开可以 drop 的 UIElement 时触发的事件
 *
 *     
 * 注:关于 ListView 和 GridView 的 Item 的 drag & drop 请参见 /Controls/CollectionControl/ListViewBaseDemo/ListViewBaseDemo2.xaml
 *     
 *     
 * 本例用于演示 UIElement 的 drag & drop 的基本应用
 */

using System;
using System.Collections.Generic;
using System.Linq;
using Windows.ApplicationModel.DataTransfer;
using Windows.Storage;
using Windows.Storage.Streams;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media.Imaging;

namespace Windows10.Controls.BaseControl.UIElementDemo
{
    public sealed partial class DragDropDemo1 : Page
    {
        public DragDropDemo1()
        {
            this.InitializeComponent();
        }

        // dragGrid1 开始 drag 时触发的事件
        private void dragGrid1_DragStarting(UIElement sender, DragStartingEventArgs args)
        {
            lblMsg.Text += "dragGrid1_DragStarting";
            lblMsg.Text += Environment.NewLine;

            // 通过 DataPackage 保存文本数据(关于 DataPackage 的详细说明请参见“分享”部分)
            // 一个 DataPackage 对象可以包含多种类型的数据:ApplicationLink, WebLink, Bitmap, Html, Rtf, StorageItems, Text
            args.Data.SetText(sourceTextBlock.Text);
        }

        // dragGrid1 结束 drop 时触发的事件
        private void dragGrid1_DropCompleted(UIElement sender, DropCompletedEventArgs args)
        {
            lblMsg.Text += "dragGrid1_DropCompleted";
            lblMsg.Text += Environment.NewLine;
        }

        // dragGrid2 开始 drag 时触发的事件
        private void dragGrid2_DragStarting(UIElement sender, DragStartingEventArgs args)
        {
            lblMsg.Text += "dragGrid2_DragStarting";
            lblMsg.Text += Environment.NewLine;

            RandomAccessStreamReference imageStreamRef = RandomAccessStreamReference.CreateFromUri(new Uri("ms-appx:///Assets/hololens.jpg", UriKind.Absolute));
            // 通过 DataPackage 保存图片数据
            args.Data.SetBitmap(imageStreamRef);
        }

        // dragGrid2 结束 drop 时触发的事件
        private void dragGrid2_DropCompleted(UIElement sender, DropCompletedEventArgs args)
        {
            lblMsg.Text += "dragGrid2_DropCompleted";
            lblMsg.Text += Environment.NewLine;
        }

        // 拖拽进入 dropGrid 时触发的事件
        private void dropGrid_DragEnter(object sender, DragEventArgs e)
        {
            lblMsg.Text += "dropGrid_DragEnter";
            lblMsg.Text += Environment.NewLine;

            // 指定拖拽操作的类型(None, Copy, Move, Link)
            e.AcceptedOperation = DataPackageOperation.None;

            // 根据 DataPackage 中的数据类型的不同做不同的处理(注:一个 DataPackage 中也可以同时包括各种不同类型的数据)
            if (e.DataView.Contains(StandardDataFormats.Text))
            {
                e.AcceptedOperation = DataPackageOperation.Copy;
                e.DragUIOverride.Caption = "我是文本"; // 跟随 drag 点显示的文本
            }
            else if (e.DataView.Contains(StandardDataFormats.Bitmap))
            {
                e.AcceptedOperation = DataPackageOperation.Copy;
                e.DragUIOverride.Caption = "我是图片";
            }
            else if (e.DataView.Contains(StandardDataFormats.StorageItems)) // 当从 app 外部拖拽一个或多个文件进来时,系统会自动为 DataPackage 赋值
            {
                e.AcceptedOperation = DataPackageOperation.Copy;
                e.DragUIOverride.Caption = "我是文件";
            }
        }

        // 在 dropGrid 内拖拽移动时触发的事件
        private void dropGrid_DragOver(object sender, DragEventArgs e)
        {
            // lblMsg.Text += "dropGrid_DragOver";
            // lblMsg.Text += Environment.NewLine;
        }

        // 拖拽离开 dropGrid 时触发的事件
        private void dropGrid_DragLeave(object sender, DragEventArgs e)
        {
            lblMsg.Text += "dropGrid_DragLeave";
            lblMsg.Text += Environment.NewLine;
        }

        // 在 dropGrid 内 drop 后触发的事件
        private async void dropGrid_Drop(object sender, DragEventArgs e)
        {
            lblMsg.Text += "dropGrid_Drop";
            lblMsg.Text += Environment.NewLine;

            if (e.DataView.Contains(StandardDataFormats.Text))
            {
                // 获取 DataPackage 中的文本数据
                string text = await e.DataView.GetTextAsync();
                targetTextBlock.Text += text;
                targetTextBlock.Text += Environment.NewLine;
            }
            else if (e.DataView.Contains(StandardDataFormats.Bitmap))
            {
                // 获取 DataPackage 中的图片数据
                RandomAccessStreamReference imageStreamRef = await e.DataView.GetBitmapAsync();
                IRandomAccessStream imageStream = await imageStreamRef.OpenReadAsync();
                BitmapImage bitmapImage = new BitmapImage();
                bitmapImage.SetSource(imageStream);
                targetImage.Source = bitmapImage;
            }
            else if (e.DataView.Contains(StandardDataFormats.StorageItems))
            {
                // 获取 DataPackage 中的文件数据(当从 app 外部拖拽一个或多个文件进来时,系统会自动为 DataPackage 赋值)
                IReadOnlyList<IStorageItem> items = await e.DataView.GetStorageItemsAsync();
                foreach (var storageFile in items.OfType<StorageFile>())
                {
                    if (storageFile != null)
                    {
                        targetTextBlock.Text += storageFile.Path;
                        targetTextBlock.Text += Environment.NewLine;
                    }
                }
            }
        }
    }
}


2、演示如何手动开启 UIElement 的拖放操作
Controls/BaseControl/UIElementDemo/DragDropDemo2.xaml

<Page
    x:Class="Windows10.Controls.BaseControl.UIElementDemo.DragDropDemo2"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:Windows10.Controls.BaseControl.UIElementDemo"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <Grid Background="Transparent">
        <StackPanel Margin="5">

            <Grid Name="dragGrid" Background="Orange" Margin="5"
                  PointerMoved="dragGrid_PointerMoved"
                  DragStarting="dragGrid_DragStarting">
                <TextBlock Name="sourceTextBlock" Text="i am webabcd" Margin="20" />
            </Grid>

            <Grid Name="dropGrid" Background="Blue" Margin="5"
                  AllowDrop="True"
                  Drop="dropGrid_Drop"
                  DragEnter="dropGrid_DragEnter">
                <TextBlock Name="targetTextBlock" TextWrapping="Wrap" Height="120" Margin="20" />
            </Grid>

            <TextBlock Name="lblMsg" Margin="5" />

        </StackPanel>
    </Grid>
</Page>

Controls/BaseControl/UIElementDemo/DragDropDemo2.xaml.cs

/*
 * UIElement - UIElement(继承自 DependencyObject, 请参见 /Controls/BaseControl/DependencyObjectDemo/)
 *     StartDragAsync(PointerPoint pointerPoint) - 将 UIElement 拖拽到指定的 PointerPoint 位置,返回一个 DataPackageOperation 类型的枚举(None, Copy, Move, Link)
 * 
 * 
 * CanDrag - 由系统决定何时开启拖放操作,一般就是鼠标按下后进行拖拽
 * StartDragAsync() - 由开发者手动决定何时何地开启拖放操作
 *     
 *     
 * 本例用于演示如何手动开启 UIElement 的拖放操作
 */

using System;
using Windows.ApplicationModel.DataTransfer;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Input;

namespace Windows10.Controls.BaseControl.UIElementDemo
{
    public sealed partial class DragDropDemo2 : Page
    {
        public DragDropDemo2()
        {
            this.InitializeComponent();
        }

        private void dragGrid_DragStarting(UIElement sender, DragStartingEventArgs args)
        {
            args.Data.SetText(sourceTextBlock.Text);
        }

        private void dropGrid_DragEnter(object sender, DragEventArgs e)
        {
            e.AcceptedOperation = DataPackageOperation.Copy;
            e.DragUIOverride.Caption = "我是文本";
        }

        private async void dropGrid_Drop(object sender, DragEventArgs e)
        {
            string text = await e.DataView.GetTextAsync();
            targetTextBlock.Text += text;
            targetTextBlock.Text += Environment.NewLine;
        }

        private async void dragGrid_PointerMoved(object sender, PointerRoutedEventArgs e)
        {
            // 通过 StartDragAsync() 开启拖放操作,拖放操作的其他部分遵循相同的模式
            DataPackageOperation dpo = await dragGrid.StartDragAsync(e.GetCurrentPoint(dragGrid));
            if (dpo != DataPackageOperation.None)
            {
                targetTextBlock.Text += dpo;
                targetTextBlock.Text += Environment.NewLine;
            }
        }
    }
}



OK
[源码下载]

相关实践学习
Github实时数据分析与可视化
基于Github Archive公开数据集,将项目、行为等20+种事件类型数据实时采集至Hologres进行分析,并搭建可视化大屏。
阿里云实时数仓实战 - 项目介绍及架构设计
课程简介 1)学习搭建一个数据仓库的过程,理解数据在整个数仓架构的从采集、存储、计算、输出、展示的整个业务流程。 2)整个数仓体系完全搭建在阿里云架构上,理解并学会运用各个服务组件,了解各个组件之间如何配合联动。 3&nbsp;)前置知识要求 &nbsp; 课程大纲 第一章&nbsp;了解数据仓库概念 初步了解数据仓库是干什么的 第二章&nbsp;按照企业开发的标准去搭建一个数据仓库 数据仓库的需求是什么 架构 怎么选型怎么购买服务器 第三章&nbsp;数据生成模块 用户形成数据的一个准备 按照企业的标准,准备了十一张用户行为表 方便使用 第四章&nbsp;采集模块的搭建 购买阿里云服务器 安装 JDK 安装 Flume 第五章&nbsp;用户行为数据仓库 严格按照企业的标准开发 第六章&nbsp;搭建业务数仓理论基础和对表的分类同步 第七章&nbsp;业务数仓的搭建&nbsp; 业务行为数仓效果图&nbsp;&nbsp;
目录
相关文章
|
1月前
|
Linux C++ Windows
【Azure 应用服务】Azure App Service(Windows)环境中如何让.NET应用调用SAP NetWeaver RFC函数
【Azure 应用服务】Azure App Service(Windows)环境中如何让.NET应用调用SAP NetWeaver RFC函数
【Azure 应用服务】Azure App Service(Windows)环境中如何让.NET应用调用SAP NetWeaver RFC函数
|
1月前
|
Unix Linux Ruby
在windows和linux上高效快捷地发布Dash应用
在windows和linux上高效快捷地发布Dash应用
|
1月前
|
Linux iOS开发 开发者
跨平台开发不再难:.NET Core如何让你的应用在Windows、Linux、macOS上自如游走?
【8月更文挑战第28天】本文提供了一份详尽的.NET跨平台开发指南,涵盖.NET Core简介、环境配置、项目结构、代码编写、依赖管理、构建与测试、部署及容器化等多个方面,帮助开发者掌握关键技术与最佳实践,充分利用.NET Core实现高效、便捷的跨平台应用开发与部署。
60 3
|
1月前
|
PHP Windows
【Azure App Service for Windows】 PHP应用出现500 : The page cannot be displayed because an internal server error has occurred. 错误
【Azure App Service for Windows】 PHP应用出现500 : The page cannot be displayed because an internal server error has occurred. 错误
|
28天前
|
vr&ar C# 图形学
WPF与AR/VR的激情碰撞:解锁Windows Presentation Foundation应用新维度,探索增强现实与虚拟现实技术在现代UI设计中的无限可能与实战应用详解
【8月更文挑战第31天】增强现实(AR)与虚拟现实(VR)技术正迅速改变生活和工作方式,在游戏、教育及工业等领域展现出广泛应用前景。本文探讨如何在Windows Presentation Foundation(WPF)环境中实现AR/VR功能,通过具体示例代码展示整合过程。尽管WPF本身不直接支持AR/VR,但借助第三方库如Unity、Vuforia或OpenVR,可实现沉浸式体验。例如,通过Unity和Vuforia在WPF中创建AR应用,或利用OpenVR在WPF中集成VR功能,从而提升用户体验并拓展应用功能边界。
33 0
|
28天前
|
存储 开发者 C#
WPF与邮件发送:教你如何在Windows Presentation Foundation应用中无缝集成电子邮件功能——从界面设计到代码实现,全面解析邮件发送的每一个细节密武器!
【8月更文挑战第31天】本文探讨了如何在Windows Presentation Foundation(WPF)应用中集成电子邮件发送功能,详细介绍了从创建WPF项目到设计用户界面的全过程,并通过具体示例代码展示了如何使用`System.Net.Mail`命名空间中的`SmtpClient`和`MailMessage`类来实现邮件发送逻辑。文章还强调了安全性和错误处理的重要性,提供了实用的异常捕获代码片段,旨在帮助WPF开发者更好地掌握邮件发送技术,提升应用程序的功能性与用户体验。
33 0
|
28天前
|
C# Windows 监控
WPF应用跨界成长秘籍:深度揭秘如何与Windows服务完美交互,扩展功能无界限!
【8月更文挑战第31天】WPF(Windows Presentation Foundation)是 .NET 框架下的图形界面技术,具有丰富的界面设计和灵活的客户端功能。在某些场景下,WPF 应用需与 Windows 服务交互以实现后台任务处理、系统监控等功能。本文探讨了两者交互的方法,并通过示例代码展示了如何扩展 WPF 应用的功能。首先介绍了 Windows 服务的基础知识,然后阐述了创建 Windows 服务、设计通信接口及 WPF 客户端调用服务的具体步骤。通过合理的交互设计,WPF 应用可获得更强的后台处理能力和系统级操作权限,提升应用的整体性能。
64 0
|
1月前
|
网络安全 API 数据安全/隐私保护
【Azure App Service】.NET代码实验App Service应用中获取TLS/SSL 证书 (App Service Windows)
【Azure App Service】.NET代码实验App Service应用中获取TLS/SSL 证书 (App Service Windows)
|
1月前
|
Python Windows 内存技术
【Azure 应用服务】Azure App Service (Windows) 使用Flask框架部署Python应用,如何在代码中访问静态文件呢?如何设置文件路径?是相对路径还是绝对路径呢?
【Azure 应用服务】Azure App Service (Windows) 使用Flask框架部署Python应用,如何在代码中访问静态文件呢?如何设置文件路径?是相对路径还是绝对路径呢?
|
1月前
|
Windows
Windows——如何提取Microsoft Store的应用
Windows——如何提取Microsoft Store的应用
27 0