在WinForms拖放中的e.Data.GetData转换问题 [英] e.Data.GetData casting problem in WinForms drag-and-drop

查看:112
本文介绍了在WinForms拖放中的e.Data.GetData转换问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在我的程序的两个实例之间进行拖放,但是当我尝试将数据投射到我的类型时,会抛出一个无效的Cast异常。

i'm trying to make a drag-and-drop between 2 instances of my programs, but when i try to cast data to my type it throws an Invalid Cast Exception.

这是代码:

    protected virtual void GetDropIEntities(DragEventArgs e)
    {

        foreach (string s in e.Data.GetFormats())
            Console.WriteLine(s);

        Entity[] myDroppedEnts = (Entity[])e.Data.GetData(e.Data.GetFormats()[0]);

    }

最奇怪的是Console.WriteLine写入Entity [ ]的输出,但是当我尝试投射异常抛出。有人可以给我这个手吗? Thx提前!

The weirdest thing is that Console.WriteLine writes "Entity[]" on the output, but when i try to cast the exception is thrown. Can someone give me a hand with this? Thx in advance!

推荐答案

要将对象从应用程序的一个实例拖放到另一个实例,对象必须是序列化即可。否则,演员将无法按预期方式工作。

To drag and drop objects from one instance of an application to another, the object must be serializable. Otherwise, the cast will not work as expected.

要完成此操作,您可以添加 [Serializable] 属性到您的实体类,并且可选地实现 ISerializable 接口。有关如何使类Serializable的介绍,请参阅: http://msdn.microsoft.com/en-us/library/4abbf6k0(v = VS.90).aspx

To accomplish this, you can add the [Serializable] attribute to your Entity class, and optionally implement the ISerializable interface. For an introduction on how to make a class Serializable, see: http://msdn.microsoft.com/en-us/library/4abbf6k0(v=VS.90).aspx

这是一样的使用剪贴板问题。在这个CodeProject网站上可以看到一个很好的例子:

This is the same issue with using the Clipboard. A great example can be seen on this CodeProject site:

http://www.codeproject.com/KB/cs/copycustomclasstoclipbrd.aspx

这篇关于在WinForms拖放中的e.Data.GetData转换问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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