从剪贴板中比较IDataObject的 [英] Comparing IDataObject from the Clipboard

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

问题描述

我的WPF应用程序检查剪贴板上的数据,看它是否能够与数据或无法正常工作。因为我设置启用某些按钮/禁用根据数据(通过的ICommand 执行),这code频繁调用。

My WPF application checks the data on the clipboard to see if it can work with the data or not. Because I set some buttons to be enabled/disabled based on the data (via an ICommand implementation), this code is called frequently.

的工作,以确定是否我的程序就可以与所述数据可以是不平凡的,有时,这样导致我的申请挂起随机。我不相信我可以把这个工作给另一个线程,因为WPF运行时快速地期望得到响应。

The work to determine if my application can work with the data can be non-trivial at times, and thus is causing my application to "hang" randomly. I don't believe I can push this work off to another thread since the WPF runtime is expecting a response quickly.

为了解决这个问题,我想我会比较的IDataObject S(当前的从剪贴板与缓存一个来自previous尝试一个比较直的(甚至是 object.ReferenceEquals 不返回预期的效果,所以我想我会尝试的方法 Clipboard.IsCurrent 的描述听起来像正是我想要的,但是当我评价如下:

In order to solve this issue, I thought I would compare the IDataObjects (the current one from the clipboard vs. a cached one from the previous attempt. A straight comparison (and even an object.ReferenceEquals does not return the desired results, so I thought I would try the method Clipboard.IsCurrent. The description sounds like exactly what I want, but when I evaluate the following:

Clipboard.IsCurrent(Clipboard.GetDataObject())

的结果是。目前的解决方法是比较对IDataObject的数据格式,但是这不是因为我的应用程序可以处理从文件系统的一些文件一个很好的答案,但不是全部。所以,即使格式是相同的,关于是否我的应用程序可以处理的数据可能并不总是相同的结果。

the result is false. The current workaround is to compare the data formats on the IDataObject, but that's not a good answer since my application can handle some files from the file system, but not all. So even though the formats are identical, the result on whether my application can handle the data may not always be the same.

推荐答案

不幸的是, IsCurrent 不结合 GetDataObject 的MSDN的描述 OleIsCurrentClipboard (其中 IsCurrent 内部使用)是相当明确这一点:

Unfortunately, IsCurrent does not work in conjunction with GetDataObject. MSDN's description of OleIsCurrentClipboard (which IsCurrent uses internally) is quite explicit about this:

OleIsCurrentClipboard仅适用于在OleSetClipboard功能中使用的数据对象。 它不能被数据对象的消费者呼吁,以确定这是否是在剪贴板上的previous OleGetClipboard调用的对象是仍然在剪贴板上。

OleIsCurrentClipboard only works for the data object used in the OleSetClipboard function. It cannot be called by the consumer of the data object to determine if the object that was on the clipboard at the previous OleGetClipboard call is still on the clipboard.

有一个解决方法可以订阅到剪贴板更新(如见 剪贴板事件C#)和评估数据,只有当它的变化,可能会在后台线程。

A workaround could be to subscribe to clipboard updates (see e.g. Clipboard event C#) and evaluate the data only when it changes, potentially in a background thread.

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

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