在另一个线程中创建 WPF 元素 [英] Creating a WPF element in another Thread

查看:28
本文介绍了在另一个线程中创建 WPF 元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以在不同线程上运行 2 个或更多 WPF 窗口.问题是现在我的应用程序拆分成多个窗口.

I'm able to run 2 or more WPF windows on different thread. The problem is that now my application in splitted in many windows.

我真正想要的是有一个包含网格的主窗口,其中每个单元格都包含一个由不同线程管理的元素.

What I really want is have a main window containg a grid in which every cell contains an element managed by a different thread.

是否可以创建由不是管理父/包含窗口的线程管理的 UIElement/组件?

Is it possible to create a UIElement/Component managed by a thread that is not the one which manage the parent/ containing window?

是否可以在某个框架/UIElement 中封装在不同线程上运行的窗口?

Is it possible to encapsulate a window that runs on a different thread in some frame/UIElement?

谢谢

是否可以使用 MediaElement 将窗口投影到面板中?

Is it possible to use a MediaElement to project a window into a Panel?

推荐答案

我真正想要的是有一个包含网格的主窗口,其中每个单元格都包含一个由不同线程管理的元素."

"What I really want is have a main window containg a grid in which every cell contains an element managed by a different thread."

解决此问题的一种方法是在单元格中正常创建元素.创建一个不接触 UI 而是在它自己的线程上运行的常规类 ViewModel.这门课是你真正想在你的细胞中做什么的背后的大脑,而不是你想在你的细胞中展示什么.当它的数据被更新时,这个 ViewModel 类应该实现 INotifyPropertyChanged.在 MainWindow.cs 文件中,您可以将单元格元素的 DataContext 设置为这些 ViewModel.最后,在您的 XAML 中,您可以将您尝试显示的内容与 ViewModel 中的属性绑定.

One way to go about this would be to create your elements normally in the cell. Create a regular class ViewModel that doesn't touch the UI but runs on it's own thread. This class is the brains behind what you're actually trying to DO with in your cells, not what you're trying to SHOW in your cells. This ViewModel class should implement INotifyPropertyChanged when it's data is has been updated. In your MainWindow.cs file you can set your cell elements' DataContext to these ViewModels. Lastly, in your XAML you can Bind things you're trying to show with the Properties in your ViewModel.

我知道我对很多细节感到轻松,但这只是一个起点.如果您需要任何帮助,这里有很多帮助.

I know I breezed over a lot of details, but it's a starting point. Lots of help to be had around here if you need any.

这篇关于在另一个线程中创建 WPF 元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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