剪贴板复制对象,并从 [英] Clipboard Copying objects to and from

查看:199
本文介绍了剪贴板复制对象,并从的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图到Windows剪贴板,然后再次关闭复制的对象。我的代码是这样的:

I am trying to copy an object onto the windows clipboard and off again. My code is like this:

在剪贴板复制:

Clipboard.Clear();
DataObject newObject = new DataObject(prompts);
newObject.SetData(myString);
Clipboard.SetDataObject(newObject);



其中,提示列表< Data.Sources.PromptResult>

复制关闭剪贴板。

IDataObject dataObject = System.Windows.Forms.Clipboard.GetDataObject();
if (dataObject.GetDataPresent(typeof(List<Data.Sources.PromptResult>)))
{
  Type type = typeof(List<Data.Sources.PromptResult>);
  Object obj = dataObject.GetData(type);
  return (List<Data.Sources.PromptResult>)dataObject.GetData(type);
}



GetFormats()显示格式列表中是和 GetDataPresent(列表< Data.Sources.PromptResult>)返回真实的,但如果我尝试获取对象出了<$与剪贴板类>的GetData(列表< Data.Sources.PromptResult>)。我得到返回null

The GetFormats() shows the format as being in the list and the GetDataPresent(List<Data.Sources.PromptResult>) returns true but if I try to get the object out of the Clipboard class with GetData(List<Data.Sources.PromptResult>) I get a return of null.

有没有人有任何想法可能是错误的?

Does anyone have any idea what might be wrong?

推荐答案

@Reniuz感谢您帮助它帮助我制定出答案。

@Reniuz thanks for your help it has helped me to work out the answer.

为了得到文本和自定义对象的数据输出多种格式的剪贴板我在实现IDataObject接口的我自己的类。设置数据对象的代码必须这样设置复制标记:

In order to get the text and custom object data out of the Clipboard with multiple formats I have implemented the IDataObject interface in my own class. The code to set the data object must have the copy flag set like this:

Clipboard.Clear();
Clipboard.SetDataObject(myClassThatImplementsIDataObject, true);

要出一次标准文本可以使用检索得到的数据:

To get the data out again the standard text can be retrieved using:

Clipboard.GetText();



中的数据可以通过数据的方法获得:

The data can be retrieved using the data method:

Clipboard.GetData("name of my class");



另外一点,这是有帮助的是测试我把到剪贴板的对象可以被序列化通过使用BinaryFormatter类来执行这个测试......如果一个异常被抛出的复制到剪贴板也将失败,只是默默地

The other point that was helpful was to test that the object I was putting into the clipboard could be serialized by using the BinaryFormatter class to perform this test... If an exception is thrown that copying to the clipboard would also fail, but silently.

所以我的类有:

[Serializable]
public class ClipboardPromptsHolder : IDataObject
{
    ...
}

这篇关于剪贴板复制对象,并从的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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