简体   繁体   English

如何将元组复制到剪贴板

[英]How to copy Tuples to clipboard

I have to copy three different data to the clipboard.我必须将三个不同的数据复制到剪贴板。 So I create a Tuple and copy it to the clipboard and then I copy it to clipboard所以我创建了一个元组并将其复制到剪贴板,然后将其复制到剪贴板

var newTuple = new Tuple<Component, Color?, bool?>(CopyComponent, headerColour_Copied, IsHeaderForegroundDark_Copied);
Clipboard.SetDataObject(newTuple);

when copying I get an exception which translated may sound like "Other data are available".复制时出现异常,翻译后可能听起来像“其他数据可用”。 I also tried to purge the clipboard but that was of no help.我也试图清除剪贴板,但这没有帮助。

Clipboard.SetDataObject(CopyComponent);
Clipboard.SetDataObject(headerColour_Copied);
Clipboard.SetDataObject(IsHeaderForegroundDark_Copied);

Please notice that if I don't make a Tuple but set each datum separately everything is fine.请注意,如果我不制作元组而是分别设置每个数据,一切都很好。

So can't tuples be copied to clipboard??那么元组不能复制到剪贴板吗?

I also tried to box each of the three datum into an object without but still fails我还尝试将三个数据中的每一个都装箱到 object 中,但仍然失败

Thanks for helping感谢您的帮助

Patrick帕特里克

Generic types typically don't play very well outside the realm of.泛型类型通常在 realm 之外不能很好地发挥作用。 net;网; try sending a plain array of values instead:尝试发送一个普通的值数组:

Clipboard.SetDataObject(new object[] { CopyComponent, headerColour_Copied, IsHeaderForegroundDark_Copied });

Clipboard content can be consumed by any application;剪贴板内容可以被任何应用程序使用; basic intrinsic types are a safer bet here.基本的内在类型在这里是更安全的选择。

Fortunately, we can store ValueTuple or Tuple on the Clipboard and retrieve it as a serializable data object.幸运的是,我们可以将ValueTupleTuple存储在剪贴板上,并将其作为可序列化数据 object 检索。

// using System.Windows;
public void TupleClipboardTest1()
{
    (int, string) original = (11, "Eleven");

    Clipboard.SetData(DataFormats.Serializable, original);

    object retrieved = Clipboard.GetData(DataFormats.Serializable);

    Debug.WriteLine(retrieved.GetType()); // System.ValueTuple`2[System.Int32,System.String]

    (int, string) restored = (ValueTuple<int, string>)retrieved;

    Debug.WriteLine(restored.Item1); // 11
    Debug.WriteLine(restored.Item2); // Eleven
}
// using System.Windows;
public void TupleClipboardTest2()
{
    Tuple<int, string> original = (11, "Eleven").ToTuple();

    Clipboard.SetData(DataFormats.Serializable, original);

    object retrieved = Clipboard.GetData(DataFormats.Serializable);

    Debug.WriteLine(retrieved.GetType()); // System.Tuple`2[System.Int32, System.String]

    Tuple<int, string> restored = (Tuple<int, string>)retrieved;

    Debug.WriteLine(restored.Item1); // 11
    Debug.WriteLine(restored.Item2); // Eleven
}

Of cource, its items must be serializable as well.当然,它的项目也必须是可序列化的。 Otherwise it causes a COMException.否则会导致 COMException。

// using System.Windows;
public void TupleClipboardTest3()
{
    (int, System.Windows.Media.Color) original = (11, System.Windows.Media.Colors.Gray);

    Clipboard.SetData(DataFormats.Serializable, original);

    object retrieved = Clipboard.GetData(DataFormats.Serializable); // System.Runtime.InteropServices.COMException: 'Data on clipboard is invalid (0x800401D3 (CLIPBRD_E_BAD_DATA))'
}

Therefore, we need to convert an non-serializable item to a serializable object beforehand.因此,我们需要预先将不可序列化的项目转换为可序列化的 object。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM