问题描述
大家好,
我正在使用UWP中的应用程序。我意识到拖放没有按预期工作,所以我最终创建了一个新的UWP应用程序并尝试拖放那里的东西。
我可以让DragEnter开火,但我无法让Drop事件发挥作用。无论我拖动什么,我总是得到一个停止标志符号,如果我在Drop事件上设置一个断点,它就不会触发。
这是我的代码:
Hi all,
I'm working on an app in UWP. I realized that drag and drop didn't work as expected so I eventually created a new UWP app and try to drag and drop things there.
I can get DragEnter to fire, but I can't get the Drop event to work. Whatever I drag, I always get a "stop sign" symbol, and if I set a breakpoint on the Drop event it doesn't fire.
Here's my code:
<Page
x:Class="DragDropExample.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:DragDropExample"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid x:Name="tb" removed="#FFFD3535">
<TextBlock x:Name="txtDrag" HorizontalAlignment="Left" Margin="10,136,0,0" TextWrapping="Wrap" Text="This is just a test" VerticalAlignment="Top" CanDrag="True" Foreground="#FF487EF1"/>
<TextBox x:Name="tbDrop" removed="#FF36DA12" Width="100" Height="100" AllowDrop="True" Drop="tbDrop_Drop" DragEnter="tbDrop_DragEnter" />
<Grid x:Name="gridDrop" HorizontalAlignment="Left" Margin="100, 100, 0, 0" removed="#FFE81DF1" AllowDrop="True" Height="100" Width="100" VerticalAlignment="Bottom" />
<Canvas x:Name="canvasDrag" HorizontalAlignment="Left" Height="50" Margin="230,10,0,0" VerticalAlignment="Top" Width="50" Background="#FFF7F71A" CanDrag="True" Drop="canvasDrag_Drop"/>
</Grid>
</Page>
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.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;
// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409
namespace DragDropExample
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
}
private void tbDrop_Drop(object sender, DragEventArgs e)
{
tbDrop.Text = "Dropped";
}
private void tbDrop_DragEnter(object sender, DragEventArgs e)
{
tbDrop.Text = "Entered";
}
private void canvasDrag_Drop(object sender, DragEventArgs e)
{
tbDrop.Text = "Dropped on canvas";
}
}
}
我缺少什么?
我也想知道如何实际拖动对象而不是仅仅复制它们。 (文本块被拖动,但拖动的是副本,原始文本块仍然存在。)
请注意示例代码很傻 - 我只需要有效的东西,不涉及列表视图中的项目或类似的东西。我希望能够拖动带有图像的图像或画布(用于国际象棋游戏)。
谢谢!
Petter
What am I missing?
I also wonder how I can actually drag the objects rather than just making copies of them. (The textblock gets dragged, but what is dragged is a copy, and the original textblock is still there.)
Note that the example code is silly - I just need something that works and that doesn't involve items in a listview or similiar. Preferably, I'd like to be able to drag images or canvases with images in them (for a chess game).
Thanks!
Petter
推荐答案
private void tbDrop_DragEnter(object sender, DragEventArgs e)
{
// Check here if data can be generally dropped:
// - Control is active and not read only
// - Drag source provides data of a matching type (e.g. a canvas and not text)
// The result may be stored in a class member variable so the check
// must not be executed again with each DragOver event.
// When using such a variable, it must be cleared inside the
// Drop and DragLeave handlers.
this.canDrop = CanDrop(sender, e);
// Call DragOver to check at the actual position and set the effect.
DragOver(sender, e);
}
private void tbDrop_DragOver(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.None;
// Check here if dropping is possible in general and at the actual position.
if (this.canDrop && CanDropHere(sender, e))
{
// See also MSDN example code for the Effect Property
if ((e.KeyState & 8) == 8 &&
(e.AllowedEffect & DragDropEffects.Copy) == DragDropEffects.Copy)
{
// CTL KeyState for copy.
e.Effect = DragDropEffects.Copy;
}
else
{
// Move by default.
e.Effect = DragDropEffects.Move;
}
}
}
private void tbDrop_Drop(object sender, DragEventArgs e)
{
// DragOver() has been called by the system just before this and
// the effect has been set.
if (e.Effect != DragDropEffects.None)
{
// Drop data here.
// ...
}
// Cleanup (similar code as in DragLeave()).
this.canDrop = false;
}
private void tbDrop_DragLeave(object sender, DragEventArgs e)
{
// Cleanup when leaving.
this.canDrop = false;
}
在拖动源端使用 DropCompleted
处理程序检测是否已发生丢弃以及是否应移动或复制数据。
On the drag source side use the DropCompleted
handler to detect if dropping has been occured and if data should be moved or copied.
private void tbDragSource_DropCompleted(UIElement elem, DropCompletedEventArgs e)
{
if (e.DropResult == DataPackageOperation.Move)
{
// Delete item here
}
}
[/ UPDATE]
[/UPDATE]
这篇关于为什么Drop事件不会在我的UWP测试应用程序中触发?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!