c# - Clipboard.ContainsData 和 Clipboard.GetData

标签 c# .net vb.net clipboard

我尝试使用 Clipboard 类粘贴一些复制的对象。

  <Serializable()> Public Class DogsZoo
    Public Property Dogs As List(Of Dog)
    Public Property Workers As List(Of Worker)

    Public Sub New(dogs As List(Of Dog), workers As List(Of Worker))
      Me.Dogs = dogs
      Me.Workers = workers
    End Sub
  End Class

Dim myDogsZoo = myCity.GetDogsZoo()
Clipboard.SetData("dogs", myDogsZoo)

' bla bla , some actions '

If Not Clipboard.ContainsData("dogs") Then Throw New Exception("Clipboard")

' here I obtain Nothing !?'
Dim clipboardObject = Clipboard.GetData("dogs")

验证 Clipboard.ContainsData(myFormat) 成功通过,但是当我尝试获取数据时,我获取的是 null(无)。这是正确的行为吗?

附言。
C# 或 VB.NET 答案都可以。

PPS.
我应该认识到,当数据类型很简单(通用列表)时,我使用上面的方法对剪贴板没有任何问题。现在,我将要保存在内存中的对象更改为自定义对象...从那时起...这个问题...

最佳答案

如果您使用自定义对象,我相信该对象必须支持序列化。

我在调查从剪贴板读取图像和文本时发现以下文章很有用: http://msdn.microsoft.com/en-us/library/637ys738.aspx

有一节是关于以自定义格式将数据写入剪贴板。我注意到您在 VB.net 中编写代码,因此我复制了 VB.net 摘录如下:

' Demonstrates SetData, ContainsData, and GetData ' using a custom format name and a business object. Public ReadOnly Property TestCustomFormat() As Customer Get Clipboard.SetData("CustomerFormat", New Customer("Customer Name"))

    If Clipboard.ContainsData("CustomerFormat") Then
        Return CType(Clipboard.GetData("CustomerFormat"), Customer)
    End If

    Return Nothing
End Get End Property

...

Public Class Customer

Private nameValue As String = String.Empty

Public Sub New(ByVal name As String)
    nameValue = name
End Sub

Public Property Name() As String
    Get
        Return nameValue
    End Get
    Set(ByVal value As String)
        nameValue = value
    End Set
End Property

End Class

希望这有帮助吗?

亲切的问候,韦恩

关于c# - Clipboard.ContainsData 和 Clipboard.GetData,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8525139/

相关文章:

c# - DTO 到实体映射工具

.net - 使用 Datetime.Now 具有不同文件名的 TPL

.net - 为什么不能用 .net 中的读/写属性覆盖只读属性?

.net - Visual Basic 如何到达 %appdata%

c# - 数据绑定(bind)到工具提示

c# - String.Format 函数不起作用

c# - 在 C# 控制台应用程序中编辑文本?

c# - 如何使用 FileOpenDialog 在 C# 中打开文件

c# - 如何找到两组不连续时间的交集?

c# - 在代码中填充 LINQ 表?