WPF - 我正在使用BackgroundWorker创建一个Model3D对象,但是当我想将其添加到在XAML中定义的Model3DGroup中时,我会遇到异常:
无法使用属于不同线程而非其父Freezable的DependencyObject。
这是整个代码背后的部分:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
BackgroundWorker bw = new BackgroundWorker();
bw.DoWork += bw_DoWork;
bw.RunWorkerCompleted += bw_RunWorkerCompleted;
bw.RunWorkerAsync();
}
private void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
GeometryModel3D geometryModel3D = (GeometryModel3D)e.Result;
model3DGroup.Children.Add(geometryModel3D);
}
private void bw_DoWork(object sender, DoWorkEventArgs e)
{
GeometryModel3D geometryModel3D = new GeometryModel3D();
e.Result = geometryModel3D;
}
}
以下是整个XAML代码:
<Grid>
<Viewport3D Margin="4,4,4,4" Grid.Row="0" Grid.Column="0">
<ModelVisual3D>
<ModelVisual3D.Content>
<Model3DGroup x:Name="model3DGroup">
</Model3DGroup>
</ModelVisual3D.Content>
</ModelVisual3D>
</Viewport3D>
</Grid>