Ill*_*dan 5 c# wpf user-interface multithreading
这是工作代码(但在 UI 线程上):
<ContentControl
Width="{Binding Path=ActualWidth, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListView}}}"
Focusable="True"
HorizontalAlignment="Right"
Content="{Binding Work}" >
</ContentControl>
Run Code Online (Sandbox Code Playgroud)
这是我试图制作的代码,但它不起作用:(代码隐藏)
InitializeComponent();
var thread = new Thread(
() =>
{
var a = new ContentControl { Focusable = true, HorizontalAlignment = HorizontalAlignment.Right };
var binding = new Binding { Path = new PropertyPath("Work"), };
a.SetBinding(ContentProperty, binding);
MainGrid.Dispatcher.Invoke(new Action(() => { MainGrid.Children.Add(a); }));
}) { IsBackground = true };
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
Run Code Online (Sandbox Code Playgroud)
我的目标是创建 aConentControl并将其放置在网格中,该网格位于listview具有多个 的a 内ContentControls。)所示的解决方案正在崩溃,因为不同的线程拥有MainGrid. 有人对此有好的想法吗?(很想在另一个线程上执行此操作,因为它ContentControl很重,例如创建布局需要 3 秒。这是一个根据参数创建自身的通用视图。)
编辑:
主要问题是在创建这些视图期间,我的整个应用程序挂起。这是非常不受欢迎的。我的目标是将此工作负载转移到另一个(或多个)其他线程。
您可以使用该属性强制在不同的线程上执行绑定IsAsync。
Content="{Binding Work, IsAsync=True}"
Run Code Online (Sandbox Code Playgroud)
这将在新线程上执行绑定Work,完成后将填充内容。您无需费心处理代码隐藏的内容。
您可以在这里找到一个很好的实际操作教程。
| 归档时间: |
|
| 查看次数: |
1527 次 |
| 最近记录: |