Clipboard.ContainsData and Clipboard.GetData

2019-07-20 03:39发布

I try to paste some copied objects using Clipboard class.

  <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")

The verification Clipboard.ContainsData(myFormat) passes successfully, but when I try to obtain the data I obtains null (Nothing). Is it a correct behavior?

PS.
C# or VB.NET answers are both OK.

PPS.
I should recognize, I used the method above without any problems with Clipboard, when the datatypes where simple (a generic List). Now, I changed the object to keep in the memory to a custom one... and from that time... this problem...

3条回答
We Are One
2楼-- · 2019-07-20 04:09

If your using a custom object, I believe the object must be support serialization.

I found the following article helpful when I was investigating reading images and text from the clipboard: http://msdn.microsoft.com/en-us/library/637ys738.aspx

There is a section about writing data to the clipboard in a custom format. I notice you code in VB.net so I've copied the VB.net extract as follows:

' 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

Hope this helps?

Kind Regards, Wayne

查看更多
冷血范
3楼-- · 2019-07-20 04:10

Be careful that both "Dog" and "Worker" class be serializable...

The object to be copied needs to be Serializable, that means, at least all its public members needs to be serializable.

If not, the clipboard operation fails silently.

Also, the thread that accedes the Clipboard should have the STA attribute...

查看更多
Emotional °昔
4楼-- · 2019-07-20 04:14

I know this is a really old post but as it does not have a solution I thought I would provide one (my initial research lead me here but I stumbled across the cause of the problem elsewhere).

To stop my object having the same problem described in this thread I had to ensure that it contained a constructor with two parameters, as shown below. Once implemented the pasting worked perfectly.

Private Sub New(ByVal info As SerializationInfo, ByVal context As StreamingContext)
  With info
    Me.Key = .GetString("Key")
    Me.Description = .GetString("Description")
    ' etc.
  End With
End Sub

Obviously the code within the With - End With block will be specific to the properties of your own custom object.

查看更多
登录 后发表回答