简体   繁体   English

WinUI3:如何知道在 UIElement.drop() 事件中删除了哪个 uielement

[英]WinUI3 : how to know which uielement was dropped in UIElement.drop() event

I'm working on a WinUI3 desktop application with c++. I was trying to perform a drag-and-drop operation.我正在使用 c++ 开发 WinUI3 桌面应用程序。我试图执行拖放操作。 When I drop a textbox on a canvas I get a drop event from the canvas. In that drop event, as a sender, we can get a reference to the canvas instance[ the UIElement on which the drop happened ], but how to know which element was dropped on the canvas?当我在 canvas 上放置一个文本框时,我从 canvas 收到一个放置事件。在那个放置事件中,作为发送者,我们可以获得对 canvas 实例的引用 [放置发生的 UIElement],但是如何知道哪个元素被丢弃在 canvas 上?

I came across this documentation on how to process drop events, but it's unclear how to pass data between dragstaring and drop events.我遇到了关于如何处理放置事件的文档,但不清楚如何在 dragstaring 和放置事件之间传递数据。

It would be of great help if you could help me understand which UIElement was dropped when we receive UIElement.drop() event如果您能帮助我了解当我们收到 UIElement.drop() 事件时哪个 UIElement 被删除,那将有很大帮助

Thank you谢谢

Although the Drag and drop documentation (which is a UWP doc, not a WinUI3 doc which in many cases cannot be found...) says虽然拖放文档(这是一个 UWP 文档,而不是在许多情况下找不到的 WinUI3 文档......)说

In most cases, the system will construct a data package for you.大多数情况下,系统会为您构造一个数据package。 The system automatically handles: Images, Text系统自动处理:图片、文字

This doesn't work for WinUI3 for some reason (maybe it depends on app identity, packaging, security, etc.), there's nothing constructed automatically in the target data object.由于某些原因(可能取决于应用程序身份、打包、安全性等),这对 WinUI3 不起作用,目标数据 object 中没有自动构建任何内容。

So you must hook the DragStarting event and put what you want in it so you will get it back in the Drop event.因此,您必须挂钩DragStarting事件并将您想要的内容放入其中,以便在Drop事件中取回它。

You can't put/get the source element itself as an object from a drag & drop operation, you must put yourself some information you find suitable about it in the Data object. D&D as a general OS feature only supports serializable or well-known data (text, image bits, a stream, etc.).您不能通过拖放操作将源元素本身作为 object 放置/获取,您必须在数据object 中放置一些您认为合适的信息。作为一般操作系统功能的 D&D 仅支持可序列化或众所周知的数据(文本、图像位、stream 等)。

To do that you can use one of the SetXXX methods of the DataPackage class , like SetText for example.为此,您可以使用DataPackage classSetXXX方法之一,例如SetText

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM