为什么 System.Windows.Forms.Clipboard.GetData/SetData 不起作用?
我正在尝试将复制/粘贴添加到编辑项目的应用程序。拥有一组选定项目的数据副本,应该能够复制它们或将它们传输到程序的另一个实例。我尝试过这个:
const string MyClipboardFormat = "MyClipboardFormat"
private void copyToolStripMenuItem_Click(object sender, EventArgs e)
{
XmlDocument xdoc;
//add data of selected items
Clipboard.SetData(MyClipboardFormat,xdoc);
}
private void pasteToolStripMenuItem_Click(object sender, EventArgs e)
{
XmlDocument xdoc = Clipboard.GetData(MyClipboardFormat) as XmlDocument;
if (xdoc == null)
throw new Exception("Clipboard does not contain MyClipboardFormat");
//read item data from xdoc
}
我用谷歌搜索过,但只找到了有关使用 GetDataObject/SetDataObject 的信息,如果我使用反射器来查看 GetData/SetData 的作用,则相当于似乎正在发生的事情。
我应该在某处注册剪贴板格式字符串吗?
I'm trying to add copy/paste to an application that edits items. Having a copy of the data for a set of selected items, should enable duplicating them or transporting them to another instance of the program. I've tried this:
const string MyClipboardFormat = "MyClipboardFormat"
private void copyToolStripMenuItem_Click(object sender, EventArgs e)
{
XmlDocument xdoc;
//add data of selected items
Clipboard.SetData(MyClipboardFormat,xdoc);
}
private void pasteToolStripMenuItem_Click(object sender, EventArgs e)
{
XmlDocument xdoc = Clipboard.GetData(MyClipboardFormat) as XmlDocument;
if (xdoc == null)
throw new Exception("Clipboard does not contain MyClipboardFormat");
//read item data from xdoc
}
I've googled but found only bits about using GetDataObject/SetDataObject, equivalent to what appears to be going on anyway, if I use reflector to look what GetData/SetData does.
Should I register the clipboard format string somewhere?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我遇到了类似的问题,为了让它工作,我必须在将对象放在剪贴板上之前序列化该对象,并在调用 Clipboard.GetData() 后将其反序列化
I had a similar problem and to get it to work, I had to serialize the object before placing it on the clipboard and unserialize it after my call to Clipboard.GetData()
您需要注册您的格式。使用
DataFormats.GeTFormat(MyClipboardFormat)
:You need to register your format. Use
DataFormats.GeTFormat(MyClipboardFormat)
: