[源码下载]

背水一战 Windows 10 (74) - 控件(控件基类): UIElement - 与 CanDrag 相关的事件, 与 AllowDrop 相关的事件

作者:webabcd

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

  • 与 CanDrag 相关的事件(DragStartingEventArgs, DropCompletedEventArgs)
  • 与 AllowDrop 相关的事件(DragEventArgs)

示例
1、演示 UIElement 的与 CanDrag 相关的事件(DragStartingEventArgs, DropCompletedEventArgs)
Controls/BaseControl/UIElementDemo/DragDropDemo3.xaml

<Page
x:Class="Windows10.Controls.BaseControl.UIElementDemo.DragDropDemo3"
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="dragGrid1" Background="Orange" Margin="5"
CanDrag="True"
DragStarting="dragGrid1_DragStarting">
<TextBlock Name="sourceTextBlock1" Text="i am sourceTextBlock1" Margin="20" />
</Grid> <Grid Name="dragGrid2" Background="Orange" Margin="5"
CanDrag="True"
DragStarting="dragGrid2_DragStarting">
<TextBlock Name="sourceTextBlock2" Text="i am sourceTextBlock2" Margin="20" />
</Grid> <Grid Name="dragGrid3" Background="Orange" Margin="5"
CanDrag="True"
DragStarting="dragGrid3_DragStarting">
<TextBlock Name="sourceTextBlock3" Text="i am sourceTextBlock3" Margin="20" />
</Grid> <Grid Name="dragGrid4" Background="Orange" Margin="5"
CanDrag="True"
DragStarting="dragGrid4_DragStarting">
<TextBlock Name="sourceTextBlock4" Text="i am sourceTextBlock4" Margin="20" />
</Grid> <Grid Name="dropGrid" Background="Blue" Margin="5"
AllowDrop="True"
Drop="dropGrid_Drop"
DragEnter="dropGrid_DragEnter">
<TextBlock Name="targetTextBlock" TextWrapping="Wrap" MinHeight="300" Margin="20" />
</Grid> </StackPanel>
</Grid>
</Page>

Controls/BaseControl/UIElementDemo/DragDropDemo3.xaml.cs

/*
* UIElement - UIElement(继承自 DependencyObject, 请参见 /Controls/BaseControl/DependencyObjectDemo/)
*
*
* DragStartingEventArgs - DragStarting 的事件参数(CanDrag 的 UIElement 触发的事件)
* Cancel - 是否取消 drag 操作
* Data - 获取一个 DataPackage 类型的对象,用于保存数据(详见“分享”部分)
* DragUI - 获取一个 DragUI 类型的对象,用于设置 drag 过程中的 ui
* GetDeferral() - 获取类型为 DragOperationDeferral 的异步操作对象,同时开始异步操作,之后通过 DragOperationDeferral 的 Complete() 通知完成异步操作
* GetPosition(UIElement relativeTo) - 获取当前 drag 的点与指定 UIELement 的相对位置(传 null 则返回相对于 app 原点的位置)
*
* DragUI - 用于设置 drag 过程中的 ui
* SetContentFromDataPackage() - 由系统根据 DataPackage 中保存的数据的类型来决定 ui
* SetContentFromBitmapImage() - 指定一个 BitmapImage 对象作为 ui,同时可以指定此 ui 相对于 drag 点的位置
* SetContentFromSoftwareBitmap() - 指定一个 SoftwareBitmap 对象作为 ui,同时可以指定此 ui 相对于 drag 点的位置
*
* DropCompletedEventArgs - DropCompleted 的事件参数(CanDrag 的 UIElement 触发的事件)
* DropResult - 获取 drop 的结果,一个 DataPackageOperation 类型的枚举(None, Copy, Move, Link)
*
*
* 本例用于演示 UIElement 的与 CanDrag 相关的事件(DragStartingEventArgs, DropCompletedEventArgs)
*/ using System;
using Windows.ApplicationModel.DataTransfer;
using Windows.Graphics.Imaging;
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 DragDropDemo3 : Page
{
public DragDropDemo3()
{
this.InitializeComponent();
} private void dragGrid1_DragStarting(UIElement sender, DragStartingEventArgs args)
{
args.Data.SetText(sourceTextBlock1.Text);
} private void dragGrid2_DragStarting(UIElement sender, DragStartingEventArgs args)
{
args.Data.SetText(sourceTextBlock2.Text); // 由系统根据 DataPackage 中保存的数据的类型来决定 drag 过程中的 ui
args.DragUI.SetContentFromDataPackage();
} private void dragGrid3_DragStarting(UIElement sender, DragStartingEventArgs args)
{
args.Data.SetText(sourceTextBlock3.Text); // drag 过程中的 ui 为指定的 BitmapImage
args.DragUI.SetContentFromBitmapImage(new BitmapImage(new Uri("ms-appx:///Assets/hololens.jpg", UriKind.Absolute)));
} private async void dragGrid4_DragStarting(UIElement sender, DragStartingEventArgs args)
{
args.Data.SetText(sourceTextBlock4.Text); // 获取异步操作对象
DragOperationDeferral deferral = args.GetDeferral(); // 将 dragGrid4 截图,并以此创建一个 SoftwareBitmap 对象
RenderTargetBitmap rtb = new RenderTargetBitmap();
await rtb.RenderAsync(dragGrid4);
IBuffer buffer = await rtb.GetPixelsAsync();
SoftwareBitmap bitmap = SoftwareBitmap.CreateCopyFromBuffer(buffer, BitmapPixelFormat.Bgra8, rtb.PixelWidth, rtb.PixelHeight, BitmapAlphaMode.Premultiplied); // drag 过程中的 ui 为指定的 SoftwareBitmap
args.DragUI.SetContentFromSoftwareBitmap(bitmap); // 完成异步操作
deferral.Complete();
} 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;
}
}
}

2、演示 UIElement 的与 AllowDrop 相关的事件(DragEventArgs)
Controls/BaseControl/UIElementDemo/DragDropDemo4.xaml

<Page
x:Class="Windows10.Controls.BaseControl.UIElementDemo.DragDropDemo4"
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"
CanDrag="True"
DragStarting="dragGrid_DragStarting">
<TextBlock Name="sourceTextBlock" Text="i am webabcd" Margin="20" />
</Grid> <Grid Name="dropGrid1" Background="Blue" Margin="5"
AllowDrop="True"
Drop="dropGrid1_Drop"
DragEnter="dropGrid1_DragEnter">
<TextBlock Name="targetTextBlock1" TextWrapping="Wrap" Height="80" Margin="20" />
</Grid> <Grid Name="dropGrid2" Background="Blue" Margin="5"
AllowDrop="True"
Drop="dropGrid2_Drop"
DragEnter="dropGrid2_DragEnter">
<TextBlock Name="targetTextBlock2" TextWrapping="Wrap" Height="80" Margin="20" />
</Grid> <Grid Name="dropGrid3" Background="Blue" Margin="5"
AllowDrop="True"
Drop="dropGrid3_Drop"
DragEnter="dropGrid3_DragEnter">
<TextBlock Name="targetTextBlock3" TextWrapping="Wrap" Height="80" Margin="20" />
</Grid> <Grid Name="dropGrid4" Background="Blue" Margin="5"
AllowDrop="True"
Drop="dropGrid4_Drop"
DragEnter="dropGrid4_DragEnter">
<TextBlock Name="targetTextBlock4" TextWrapping="Wrap" Height="80" Margin="20" />
</Grid> </StackPanel>
</Grid>
</Page>

Controls/BaseControl/UIElementDemo/DragDropDemo4.xaml.cs

/*
* UIElement - UIElement(继承自 DependencyObject, 请参见 /Controls/BaseControl/DependencyObjectDemo/)
*
*
* DragEventArgs - Drop, DragEnter, DragOver, DragLeave 的事件参数(AllowDrop 的 UIElement 触发的事件)
* AcceptedOperation - 一个 DataPackageOperation 类型的枚举,用于指定操作的类型
* None - 无操作
* Copy - 复制操作
* Move - 移动操作
* Link - 链接操作
* DataView - 获取一个 DataPackageView 类型的对象,用于获取 DataPackage 中保存的数据(详见“分享”部分)
* DragUIOverride - 获取一个 DragUIOverride 类型的对象,用于设置 drag 过程中的 ui(在 drop 区域内)。如果此时和 drag 过程中的 DragUI 有冲突的话,则以此 DragUIOverride 为准
* Handled - 是否标记为已处理
* Modifiers - 获取一个 DragDropModifiers 类型的枚举(FlagsAttribute),用于获取当前的按键状态
* None, Shift, Control, Alt, LeftButton, MiddleButton, RightButton
* GetDeferral() - 获取类型为 DragOperationDeferral 的异步操作对象,同时开始异步操作,之后通过 DragOperationDeferral 的 Complete() 通知完成异步操作
* GetPosition(UIElement relativeTo) - 获取当前 drag 的点与指定 UIELement 的相对位置(传 null 则返回相对于 app 原点的位置)
*
* DragUIOverride - 用于设置 drag 过程中的 ui(在 drop 区域内)。它包括 3 个部分,分别是 Caption, Glyph, Content
* Caption - 标题
* IsCaptionVisible - 是否显示标题
* IsGlyphVisible - 是否显示标题的左边的那个图标(这个图标会根据你的 DataPackageOperation 的不同而不同)
* IsContentVisible - 是否显示内容(就是除了 Caption 和 Glyph 之外的内容)
* Clear() - 清除 drag 过程中的 ui(但是实际测试发现并不能清除,如果需要的话还是分别设置 IsCaptionVisible, IsGlyphVisible, IsContentVisible 吧)
* SetContentFromBitmapImage() - 指定一个 BitmapImage 对象作为 ui,同时可以指定此 ui 相对于 drag 点的位置
* SetContentFromSoftwareBitmap() - 指定一个 SoftwareBitmap 对象作为 ui,同时可以指定此 ui 相对于 drag 点的位置
*
*
* 本例用于演示 UIElement 的与 AllowDrop 相关的事件(DragEventArgs)
*/ using System;
using Windows.ApplicationModel.DataTransfer;
using Windows.Graphics.Imaging;
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 DragDropDemo4 : Page
{
public DragDropDemo4()
{
this.InitializeComponent();
} private void dragGrid_DragStarting(UIElement sender, DragStartingEventArgs args)
{
args.Data.SetText(sourceTextBlock.Text);
} private void dropGrid1_DragEnter(object sender, DragEventArgs e)
{
e.AcceptedOperation = DataPackageOperation.Copy;
e.DragUIOverride.Caption = "我是文本"; targetTextBlock1.Text += e.Modifiers;
targetTextBlock1.Text += Environment.NewLine;
} private void dropGrid2_DragEnter(object sender, DragEventArgs e)
{
e.AcceptedOperation = DataPackageOperation.Copy;
e.DragUIOverride.Caption = "我是文本"; // DragUIOverride 包含 3 个部分,分别是 Caption, Glyph, Content
e.DragUIOverride.IsCaptionVisible = false;
e.DragUIOverride.IsGlyphVisible = false;
e.DragUIOverride.IsContentVisible = false; targetTextBlock2.Text += e.Modifiers;
targetTextBlock2.Text += Environment.NewLine;
} private void dropGrid3_DragEnter(object sender, DragEventArgs e)
{
e.AcceptedOperation = DataPackageOperation.Copy;
e.DragUIOverride.Caption = "我是文本"; // drag 到 drop 区域后,drag 过程中的 ui 改为指定的 BitmapImage
e.DragUIOverride.SetContentFromBitmapImage(new BitmapImage(new Uri("ms-appx:///Assets/hololens.jpg", UriKind.Absolute))); targetTextBlock3.Text += e.Modifiers;
targetTextBlock3.Text += Environment.NewLine;
} private async void dropGrid4_DragEnter(object sender, DragEventArgs e)
{
e.AcceptedOperation = DataPackageOperation.Copy;
e.DragUIOverride.Caption = "我是文本"; // 获取异步操作对象
DragOperationDeferral deferral = e.GetDeferral();
RenderTargetBitmap rtb = new RenderTargetBitmap();
await rtb.RenderAsync(dragGrid);
IBuffer buffer = await rtb.GetPixelsAsync();
SoftwareBitmap bitmap = SoftwareBitmap.CreateCopyFromBuffer(buffer, BitmapPixelFormat.Bgra8, rtb.PixelWidth, rtb.PixelHeight, BitmapAlphaMode.Premultiplied); // drag 到 drop 区域后,drag 过程中的 ui 改为指定的 SoftwareBitmap
e.DragUIOverride.SetContentFromSoftwareBitmap(bitmap); // 完成异步操作
deferral.Complete(); targetTextBlock4.Text += e.Modifiers;
targetTextBlock4.Text += Environment.NewLine;
} private async void dropGrid1_Drop(object sender, DragEventArgs e)
{
string text = await e.DataView.GetTextAsync();
targetTextBlock1.Text += text;
targetTextBlock1.Text += Environment.NewLine;
} private async void dropGrid2_Drop(object sender, DragEventArgs e)
{
string text = await e.DataView.GetTextAsync();
targetTextBlock2.Text += text;
targetTextBlock2.Text += Environment.NewLine;
} private async void dropGrid3_Drop(object sender, DragEventArgs e)
{
string text = await e.DataView.GetTextAsync();
targetTextBlock3.Text += text;
targetTextBlock3.Text += Environment.NewLine;
} private async void dropGrid4_Drop(object sender, DragEventArgs e)
{
string text = await e.DataView.GetTextAsync();
targetTextBlock4.Text += text;
targetTextBlock4.Text += Environment.NewLine;
}
}
}

OK
[源码下载]

04-26 00:52