为什么 Clipboard.SetText 不起作用?

Why Clipboard.SetText doesn't work?

我在将一些文本复制到剪贴板时遇到问题。 我的代码如下所示:

    using System;
    using System.Windows;
    namespace namespace
    {
        public class Class1
        {
            public void Method1()
            {
                Clipboard.SetText("some text");
            }
        }
}

但在线 Clipboard.SetText("..."); 有错误信息

CS0103 The name 'Clipboard' does not exist in the current context

我做错了什么?

Clipboard class 在 PresentationCore 程序集中,因此您需要添加对 PresentationCore.dll 的引用并使用适当的命名空间:

System.Windows.Clipboard.SetText("some text");

或者(正如您已经在做的那样):

using System.Windows;
.
.
.
Clipboard.SetText("some text");