WPF 调用控件

发布于 2024-09-29 03:56:53 字数 1059 浏览 0 评论 0原文

如何使用参数调用控件?我用谷歌搜索过这个,但无处可寻!

调用 ui 线程

这是我得到的错误:

附加信息:参数计数不匹配。

当我简单检查文本框控件的文本属性是否为空时,就会发生这种情况。这在 WinForms 中有效:

if (this.textboxlink.Text == string.Empty)
   SleepThreadThatIsntNavigating(5000);

它从 if 行跳转到 catch 块并向我显示该消息。

这就是我尝试调用控件的方式:

// the delegate:
private delegate void TBXTextChanger(string text);

private void WriteToTextBox(string text)
{
    if (this.textboxlink.Dispatcher.CheckAccess())
    {
        this.textboxlink.Text = text;
    }
    else
    {
        this.textboxlink.Dispatcher.Invoke(
            System.Windows.Threading.DispatcherPriority.Normal,
            new TBXTextChanger(this.WriteToTextBox));
    }
}

我做错了什么?当我只想读取控件的内容时,从什么时候开始我必须调用该控件?

How can I invoke a control with parameters? I've googled this up, but nowhere to find!

invoke ui thread

This is the error i get:

Additional information: Parameter count mismatch.

And this happens when i do a simple check whether the text property of a textbox control is empty or not. This works in WinForms:

if (this.textboxlink.Text == string.Empty)
   SleepThreadThatIsntNavigating(5000);

It jumps from this if the line to the catch block and shows me that message.

This is how i try to invoke the control:

// the delegate:
private delegate void TBXTextChanger(string text);

private void WriteToTextBox(string text)
{
    if (this.textboxlink.Dispatcher.CheckAccess())
    {
        this.textboxlink.Text = text;
    }
    else
    {
        this.textboxlink.Dispatcher.Invoke(
            System.Windows.Threading.DispatcherPriority.Normal,
            new TBXTextChanger(this.WriteToTextBox));
    }
}

What am I doing wrong? And since when do i have to invoke a control when i just want to read its content?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

森罗 2024-10-06 03:56:53

当您调用 Invoke 时,您没有指定您的参数 (text)。当调度程序尝试运行您的方法时,它没有要提供的参数,并且您会收到异常。

尝试:

this.textboxlink.Dispatcher.Invoke(
     System.Windows.Threading.DispatcherPriority.Normal,
     new TBXTextChanger(this.WriteToTextBox), text);

如果您想从文本框中读取值,一种选择是使用 lambda:

string textBoxValue = string.Empty;

this.textboxlink.Dispatcher.Invoke(DispatcherPriority.Normal, 
     new Action( () => { textBoxValue = this.textboxlink.Text; } ));

if (textBoxValue == string.Empty)
    Thread.Sleep(5000);

When you call Invoke, you're not specifying your argument (text). When the Dispatcher tries to run your method, it doesn't have a parameter to supply, and you get an exception.

Try:

this.textboxlink.Dispatcher.Invoke(
     System.Windows.Threading.DispatcherPriority.Normal,
     new TBXTextChanger(this.WriteToTextBox), text);

If you want to read the value from a text box, one option is to use a lambda:

string textBoxValue = string.Empty;

this.textboxlink.Dispatcher.Invoke(DispatcherPriority.Normal, 
     new Action( () => { textBoxValue = this.textboxlink.Text; } ));

if (textBoxValue == string.Empty)
    Thread.Sleep(5000);
池木 2024-10-06 03:56:53

Reed 是正确的,但您需要这样做的原因是 GUI 元素不是线程安全的,因此所有 GUI 操作都必须在 GUI 线程上完成,以确保正确读取内容。对于这样的读取操作来说这是必要的,这一点不太明显,但对于写入来说这是非常必要的,因此 .NET 框架只需要在 GUI 线程中完成对 GUI 的所有访问。

Reed is correct, but the reason you need to do this is that GUI elements are not thread safe and so all GUI operations have to be done on the GUI thread to ensure that the content is being read correctly. Its less obvious why this is necessary with a read operation like this but it is very necessary with writes and so the .NET framework just requires all access to the GUI to be done in the GUI thread.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文