Winform:从剪贴板检索数据时始终获得 NULL

匿名
2024-04-01T06:45:07.9233333+00:00

我已将我的类的实例存储在剪贴板中,并且从剪贴板中检索相同的实例时,始终为空。我标记了我的类 [Serializable],但仍然没有运气。我无法找出我在哪里犯了错误。这是我的代码。请看一下并告诉我我在哪里犯了错误以及要在代码中更改什么,因此我应该能够检索我存储在剪贴板中的对象。

//Save object to clipboard
private void BtnSet_Click(object sender, EventArgs e)
{
    NodeCollection ndCollection = new NodeCollection();
    TunerDetails tds = null;
    selectednode = tvCsmTuner.SelectedNode;
    if (selectednode == null) return;

    if (selectednode != null)
    {
        ndCollection.Name = selectednode.Text;
        ndCollection.Text = selectednode.Text;
        ndCollection.Tag = selectednode.Tag;

        foreach (TreeNode node in selectednode.Nodes)
        {
            ndCollection.Nodes.Add(new NodeCollection { Name = node.Text, Text = node.Text, Tag = node.Tag });
        }

        DataObject data = new DataObject();
        data.SetData(ndCollection);
        Clipboard.SetDataObject(data);

    }

    RemoveNode();

}


//Read object back from clipboard
private void BtnGet_Click(object sender, EventArgs e)
{
        DataObject retrievedData = (DataObject)Clipboard.GetDataObject();
        if (retrievedData.GetDataPresent(typeof(NodeCollection)))
        {
            NodeCollection ndCollection1 = retrievedData.GetData(typeof(NodeCollection)) as NodeCollection;
            if (ndCollection1 != null)
            {
                MessageBox.Show(ndCollection1.Name);
            }
        }
}


[Serializable]
public class NodeCollection 
{
    public NodeCollection() {
        Nodes = new List<NodeCollection>();
    }

    #region PROPERTIES
    public string Name { get; set; }
    public string Text { get; set; }
    public object Tag { get; set; }
    public List<NodeCollection> Nodes { get; set; }
    #endregion
}

下面的代码在单独的winform项目中工作正常,但是当我在实际项目中复制相同的代码时,此行返回NULL return retrievedData.GetData(typeof(NodeCollection))作为NodeCollection;

public partial class Form4 : Form
    {
        public Form4()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            var expected = new NodeCollection();
            expected.Name = "Root";
            expected.Text = "Root";
            expected.Tag = new object();
            for (int x = 0; x <= 1; x++)
            {
                expected.Nodes.Add(new NodeCollection() { Name = "Child 1", Text = "Child 1", Tag = new object() });
            }
            SetClipboardData(expected);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            var actual = GetClipboardData();
        }

        private void SetClipboardData(NodeCollection nodeCollection)
        {
            var data = new DataObject();
            data.SetData(nodeCollection);
            Clipboard.SetDataObject(data);
        }

        //Read object back from clipboard
        private NodeCollection GetClipboardData()
        {
            NodeCollection nd = null;
            var retrievedData = Clipboard.GetDataObject();
            if (retrievedData.GetDataPresent(typeof(NodeCollection)))
                nd= retrievedData.GetData(typeof(NodeCollection)) as NodeCollection;

            return nd;
        }
    }

    [Serializable]
    public class NodeCollection
    {
        public NodeCollection()
        {
            Nodes = new List<NodeCollection>();
        }

        #region PROPERTIES
        public string Name { get; set; }
        public string Text { get; set; }
        public object Tag { get; set; }
        public List<NodeCollection> Nodes { get; set; }
        #endregion
    }

请帮助我我错过了什么。谢谢

Note:此问题总结整理于: Winform: Always getting NULL when retrieve data from clipboard

开发人员技术 | Windows 窗体
0 个注释 无注释
{count} 票

问题作者接受的答案
  1. Hui Liu-MSFT 48,711 信誉分 Microsoft 外部员工
    2024-04-01T08:24:36.8933333+00:00

    我检查了您提供的代码示例,问题出在这一行

    expected.Tag = new object();  
    

    正如官方文档所说,“任何 Object 派生类型都可以分配给标签属性。如果通过 Windows 窗体设计器设置此属性,则只能分配文本“。 所以你需要用文本而不是“new object()”来分配它,这会导致问题。


    如果回复有帮助,请点击“接受答案”并点赞。

    注意:如果您想接收此线程的相关电子邮件通知,请按照我们文档中的步骤启用电子邮件通知。

    1 个人认为此答案很有帮助。
    0 个注释 无注释

0 个其他答案

排序依据: 非常有帮助

你的答案

提问者可以将答案标记为“已接受”,版主可以将答案标记为“已推荐”,这有助于用户了解答案是否解决了提问者的问题。