Clipboard.GetText返回null(空字符串) [英] Clipboard.GetText returns null (empty string)

查看:699
本文介绍了Clipboard.GetText返回null(空字符串)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的剪辑板是用文本填充,但是当我运行

My clip board is populated with text but when I run

string clipboardData = Clipboard.GetText(System.Windows.Forms.TextDataFormat.Text);

我找回一个空字符串。我把玩着各种形式的通话,包括:

I get back an empty string. I've toyed with various forms of the call including:

string clipboardData = Clipboard.GetText();
string clipboardData = Clipboard.GetText(System.Windows.Forms.TextDataFormat.UnicodeText);

但同样的结果。

but same result.

我失去了一些东西明显?

Am I missing something obvious?

感谢

推荐答案

您只能从一个STA线程访问剪贴板。里克·布鲁斯特就遇到了这个与一些重构普通编辑 - >粘贴命令,在Paint.NET。

You can only access the clipboard from an STA thread. Rick Brewster ran into this with some refactoring of the regular Edit->Paste command, in Paint.NET.

code:

IDataObject idat = null;
Exception threadEx = null;
Thread staThread = new Thread(
    delegate ()
    {
        try
        {
            idat = Clipboard.GetDataObject();
        }

        catch (Exception ex) 
        {
            threadEx = ex;            
        }
    });
staThread.SetApartmentState(ApartmentState.STA);
staThread.Start();
staThread.Join();
// at this point either you have clipboard data or an exception

code是里克。 <一href="http://forums.getpaint.net/index.php?/topic/13712-/page__view__findpost__p__226140">http://forums.getpaint.net/index.php?/topic/13712-/page__view__findpost__p__226140

更新杰森 - 海涅取得加入<$ C $好点C>()在代理以固定的暧昧方法错误。

Update: Jason Heine made a good point of adding () after delegate to fix the ambiguous method error.

这篇关于Clipboard.GetText返回null(空字符串)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆