本文介绍了Clipboard.ContainsData和Clipboard.GetData的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图粘贴使用剪贴板类的一些复制的对象。

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

验证Clipboard.ContainsData(myFormat)成功通过,但是当我试图获取数据我得到空(没有)。它是一个正确的行为?

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#或VB.NET的答案都是确定。

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

PPS。
我应该承认,我用上面的方法,无需使用剪贴板任何问题,当数据类型,其中简单的(一个通用的列表)。现在,我改变,以保持在内存中,以一个自定义的对象...,并从那个时候......这个问题......

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...

推荐答案

我知道这是一个很老的文章,但因为它没有一个解决方案,我想我会提供一(我的初步研究,导致我在这里,但我偶然发现的问题的原因在别处)。

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

显然,随着内code - 完带块将具体到自己的自定义对象的属性。

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

这篇关于Clipboard.ContainsData和Clipboard.GetData的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 09:20