创建于其他线程WPF用户控件 [英] create Wpf user controls in other thread

查看:139
本文介绍了创建于其他线程WPF用户控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建使用另一个线程一些用户控件(s)和我使用code是这样的:

I am trying to create some UserControl(s) using another thread, and I am using code like this:

    private void btnDemo_Click(object sender, RoutedEventArgs e)
    {
      Task tsk = Task.Factory.StartNew(() =>
      {
        for (int i = 0; i < 3; i++)
        {
          MyControl sprite = new MyControl();
          pnlTest.Children.Add(sprite);
        }
      });
    }

但我在该用户构造得到此异常:

But I am getting this exception in the UserControl constructor:

The calling thread must be STA, because many UI components require this.

我不知道,我现在用正确的方法来做到这一点。请,你能帮助我与此有关。

I am not sure that I am using the right approach to do this. Please, Can you help me with this.

感谢。

推荐答案

类似的问题在 - <一个href="http://stackoverflow.com/questions/2329978/the-calling-thread-must-be-sta-because-many-ui-components-require-this">The调用线程必须STA,因为许多UI组件都需要这一点。

Similar question at - The calling thread must be STA, because many UI components require this.

这篇关于创建于其他线程WPF用户控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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