如何在UWP中执行拖放操作? [英] How do I perform drag and drop in UWP?

查看:143
本文介绍了如何在UWP中执行拖放操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我正在研究我在UWP的第一个项目。我现在尝试拖放项目,但无法使其工作。由于UWP有新的拖放方式,很难找到一些代码来完成我正在寻找的东西。



我在一个更大的画布里面有小画布(他们将成为棋盘上的棋子)。所以我只想把画布拖到其他小画布的顶部(也就是说,将一块画在另一块上面移动)。



我需要在codebehind中,在我创建画布(片段)的方法中执行此操作。我正在尝试的是:





Hi all,

I'm working on my first project in UWP. I now try drag and drop items but can't get this to work. As UWP has new ways of performing drag and drop it's hard to find some code that does what I am looking for.

I have small canvases inside a larger canvas (they are going to become chess pieces on a chessboard). So I simply want to be able to drag the canvases on top of other small canvases (that is, moving a piece on top of another piece).

I need to do this in codebehind, in the method where I create the canvases (pieces). What I am trying is this:


cnv.CanDrag = true;
cnv.AllowDrop = true;
cnv.DragEnter += new DragEventHandler(cnv_DragEnter);
cnv.Drop += new DragEventHandler(cnv_DragDrop);
cnv.DragStarting += new DragEventHandler(cnv_DragStarting);
cnv.DropCompleted += new DragCompletedEventHandler(cnv_DragCompleted);





(我的DragEnter代码有效,但DragStarting的事件处理程序将无法按照上面所述的方式工作。)



我需要能够检查玩家是否可以移动有问题的画布(片段),以及当画布(片段)掉落时会发生什么 - 是移动允许,还应该删除另一件?我已经拥有了旧应用程序的逻辑 - 我只需要为我的事件提供正确的代码。



到目前为止我找到的所有示例都在listviews上工作,我有不知道该写些什么。



谢谢!



Petter



(My DragEnter code works, but the eventhandler for DragStarting won't work the way it's written above.)

I need to be able to check whether the player can move the canvas (piece) in question, and what happens when the canvas (piece) gets dropped - is the move allowed and should it remove another piece? I already have the logic from an old application - I just need the correct code for my events.

All the examples I have found so far work on listviews and I have no idea what to write really.

Thanks!

Petter

推荐答案

我不知道UWP,但我会尽力帮助。



当你在一个控件类型中使用拖放时你应该仍然区分来源和目的地。 DragEventHandler 仅适用于目的地的拖动事件。



来源(UIElement事件)

I don't know UWP but I will try to help.

While you are using drag and drop inside one control type you should still differentiate between source and destination. DragEventHandler is for drag events on the destination only.

Source (UIElement events)

  • UIElement.DragStarting event[^]
  • UIElement.DropCompleted event[^] (dropped or cancelled)


  • DragEnter(初始化)
  • DragOver(检查是否可能在可能的位置下降)
  • 删除(当可能丢弃时)
  • DragLeave(无法删除时可选清理)


这篇关于如何在UWP中执行拖放操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆