繁体   English   中英

wpf c#在单独的线程中加载用户控件

[英]wpf c# load user control in a separate thread

当用户单击列表中可用的可用项目(模块)之一时,我将使用以下代码创建所选项目(用户控件)的新实例,然后将其添加到我的tabGroupArea中。

object uc = Activator.CreateInstance(Type.GetType("myNamespace." + selectedItem.Parameter1Value), selectedItem);
Infragistics.Windows.DockManager.ContentPane contentPane = new Infragistics.Windows.DockManager.ContentPane();
contentPane.Content = uc;
tabGroupArea.Items.Add(contentPane);

我遇到的问题是,当selectedItem内有用户控件时,initializeComponent()将需要一段时间才能完成,同时应用程序将冻结并且用户无法执行任何操作,我尝试了其他方法来放置

object uc = Activator.CreateInstance(Type.GetType("myNamespace." + selectedItem.Parameter1Value), selectedItem);

在一个单独的线程(Backgroundworker,线程和委托)中,因此我可以向用户显示一个加载页面。但是我却找不到执行该操作的页面。 任何帮助,将不胜感激 。 谢谢。

请参阅此博客文章

Catel将这种方法用于PleaseWaitWindow

下面的代码做到了:

public partial class Window1 : Window
    {
        public delegate void CreateCanvasHandler(Grid parent, int index);

        public Window1()
        {
            InitializeComponent();

            int count = 10000;

            this.TestCreateAsync(count);
        }

        private void TestCreateAsync(int count)
        {
            for (int i = 0; i < count; i++)
            {
                //check the DispatecherOperation status
                this.LayoutRoot.Dispatcher.BeginInvoke(new CreateCanvasHandler(this.CreateCanvas),
                    DispatcherPriority.Background,
                    new object[2] 
                    { 
                        this.LayoutRoot,
                        i
                    });   
            }
        }

        private void CreateCanvas(Grid parent,
            int index)
        {
            Canvas canvas = new Canvas()
            {
                Width = 200,
                Height = 100
            };

            canvas.Children.Add(new TextBlock()
            {
                Text = index.ToString(),
                FontSize = 14,
                Foreground = Brushes.Black
            });

            Thread.Sleep(100);

            parent.Children.Add(canvas);
        }
    }

暂无
暂无

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

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