我用C#和XAML开发UWP应用。我尝试实现拖放功能。
这是一个简单的应用程序来演示我如何尝试做到这一点。该应用程序有两个边框 - 一个边框被拖动,第二个边框用于放置。我还写了有关要输出的事件的信息。
版本 1(对拖动的项目使用 CanDrag=true)
<Page x:Class="UWP_DragDrop.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:UWP_DragDrop"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Border x:Name="draggedItem"
Grid.Row="0"
Height="100"
Width="150"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Background="Red"
CanDrag="True"
DragStarting="draggedItem_DragStarting"
DropCompleted="draggedItem_DropCompleted">
<TextBlock Text="Drag this item"
HorizontalAlignment="Center"
VerticalAlignment="Center" />
</Border>
<Border x:Name="dropArea"
Grid.Row="1"
Height="100"
Width="150"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Background="Blue"
AllowDrop="True"
DragEnter="dropArea_DragEnter"
Drop="dropArea_Drop">
<TextBlock Text="Drop here"
HorizontalAlignment="Center"
VerticalAlignment="Center" />
</Border>
</Grid>
using System.Diagnostics;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
namespace UWP_DragDrop
{
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
}
private void draggedItem_DragStarting(UIElement sender, DragStartingEventArgs args)
{
Debug.WriteLine("draggedItem_DragStarting");
}
private void draggedItem_DropCompleted(UIElement sender, DropCompletedEventArgs args)
{
Debug.WriteLine("draggedItem_DropCompleted");
}
private void dropArea_DragEnter(object sender, DragEventArgs e)
{
Debug.WriteLine("dropArea_DragEnter");
e.AcceptedOperation = Windows.ApplicationModel.DataTransfer.DataPackageOperation.Copy;
}
private void dropArea_Drop(object sender, DragEventArgs e)
{
Debug.WriteLine("dropArea_Drop");
}
}
}
1)当我在Visual Studio for LocalMachine和Simulator中运行它时,它可以正常工作(但仅适用于鼠标输入)。在输出中,我有以下内容:
draggedItem_DragStarting dropArea_DragEnter dropArea_Drop draggedItem_DropCompleted
2)当我尝试在模拟器(触摸模式)中运行它时 - 我无法拖动项目。不触发任何事件(输出为空)
3)当我尝试在Windows 10移动模拟器中运行它时,它不起作用。我在输出中看到的是:
draggedItem_DragStarting draggedItem_DropCompleted
一旦我移动元素 - DropCompleted 事件就会触发。
版本 2(使用 StartDragAsync)
我删除了拖动项的CanDrag=true,并通过StartDragAsync开始拖动操作。
<Page x:Class="UWP_DragDrop.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:UWP_DragDrop"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Border x:Name="draggedItem"
Grid.Row="0"
Height="100"
Width="150"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Background="Red"
PointerMoved="draggedItem_PointerMoved"
DragStarting="draggedItem_DragStarting">
<TextBlock Text="Drag this item"
HorizontalAlignment="Center"
VerticalAlignment="Center" />
</Border>
<Border x:Name="dropArea"
Grid.Row="1"
Height="100"
Width="150"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Background="Blue"
AllowDrop="True"
DragEnter="dropArea_DragEnter"
Drop="dropArea_Drop">
<TextBlock Text="Drop here"
HorizontalAlignment="Center"
VerticalAlignment="Center" />
</Border>
</Grid>
using System.Diagnostics;
using Windows.ApplicationModel.DataTransfer;
using Windows.Foundation;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Input;
namespace UWP_DragDrop
{
public sealed partial class MainPage : Page
{
IAsyncOperation<DataPackageOperation> _dragOperation;
public MainPage()
{
this.InitializeComponent();
}
private void draggedItem_PointerMoved(object sender, PointerRoutedEventArgs e)
{
if (e.Pointer.IsInContact && (_dragOperation == null))
{
Debug.WriteLine("draggedItem_StartDragAsync");
_dragOperation = draggedItem.StartDragAsync(e.GetCurrentPoint(draggedItem));
_dragOperation.Completed = DragCompleted;
}
}
private void DragCompleted(IAsyncOperation<DataPackageOperation> asyncInfo, AsyncStatus asyncStatus)
{
_dragOperation = null;
Debug.WriteLine("draggedItem_DragCompleted");
}
private void draggedItem_DragStarting(UIElement sender, DragStartingEventArgs args)
{
Debug.WriteLine("draggedItem_DragStarting");
}
private void dropArea_DragEnter(object sender, DragEventArgs e)
{
Debug.WriteLine("dropArea_DragEnter");
e.AcceptedOperation = DataPackageOperation.Copy;
}
private void dropArea_Drop(object sender, DragEventArgs e)
{
Debug.WriteLine("dropArea_Drop");
}
}
}
1)桌面和模拟器(鼠标模式)工作。
2)模拟器(触摸模式)现在也可以工作。
3)Windows 10移动模拟器不起作用。DropCompleter 在再次拖动启动后立即触发。
如何使拖放在 Windows 10 移动版上工作?为什么版本 1 在方案 2 和 3 中不起作用,而版本 2 在方案 3 中不起作用?
不幸的是,模拟器和移动设备目前似乎不支持自定义拖动。详细信息请参考此已知问题。正如雷蒙德所提到的:
您可以在列表视图中拖放项目,任何 UI 元素都可以是放置目标,但您不能拖动到另一个进程或拖动自定义项。
目前,Windows 10 不支持成熟的拖放功能。