【发布时间】:2021-10-11 21:03:03
【问题描述】:
我是 WPF 新手,在更改后尝试更新主窗口 UI 时遇到问题。
我有以下 MainWindow.xaml(我会展示其中的一部分,因为它很大)
<Window
x:Class="SimpleDemo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:HelixToolkit="clr-namespace:HelixToolkit.Wpf;assembly=HelixToolkit.Wpf"
xmlns:local="clr-namespace:SimpleDemo"
Title="Plot 3D data"
Width="680"
Height="680" WindowStartupLocation="CenterScreen">
<Window.DataContext>
<local:MainViewModel/>
</Window.DataContext>
下一个重要部分是主窗口的 3D 绘图部分,我将绘图的内容与名为 Model 的 Model3D 对象绑定。
<!-- Remember to add light to the scene -->
<HelixToolkit:SunLight />
<!-- The content of this visual is defined in MainViewModel.cs -->
<ModelVisual3D Content="{Binding Path = Model, Mode=OneWayToSource,NotifyOnTargetUpdated=True, UpdateSourceTrigger=PropertyChanged}" />
<!-- You can also add elements here in the xaml -->
<HelixToolkit:GridLinesVisual3D
Width="800"
Length="800"
MajorDistance="15"
MinorDistance="15"
Thickness="0.05" />
</HelixToolkit:HelixViewport3D>
<StackPanel Grid.Row="1" Grid.Column="1" Orientation="Horizontal">
<Button Margin="5" Click="Backward_Click">Backward</Button>
<Button Margin="5" Click="Forward_Click">Forward</Button>
</StackPanel>
该程序的想法是从一个 txt 文件中读取前 500 个数据点 (x,y,z),然后使用 Helix Toolkit 从那些数据点创建一个 AddRectangularMesh()。我对多组点执行此操作,最后我得到了一个包含多个 AddRectangularMesh() 对象的网格。之后,我将最终结果显示到 HelixViewport3D。 在 UI 中,我有两个按钮前进和后退。前进按钮正在尝试从 txt 文件中绘制接下来的 500 个点。
在我的 MainWindow.xaml.cs 文件中,我调用按钮的事件处理程序,该处理程序调用 MainViewModel.cs 文件中的函数,在该文件中计算该数据点的所有函数,最终结果将保存在 Model3D 模型对象上,然后必须在 UI 上绘制。
按钮调用 Forward_click 事件并增加计数器以读取下一个数据点
<Button Margin="5" Click="Forward_Click">Forward</Button>
按下按钮后,事件处理程序被调用,但它为空,因此 UI 永远不会更新,我无法显示下一个数据点。 在我的代码中,我已经实现了事件处理程序,以监听模型的变化,因此在函数执行后模型对象将被更新,然后调用处理程序来更新 UI。
MainViewModel 类定义为:
public class MainViewModel : Window, INotifyPropertyChanged
和事件处理程序如下:
private Model3D model;
public Model3D Model
{
get { return model; }
set
{
model = value;
OnPropertyChanged(nameof(Model));
}
}
protected void OnPropertyChanged(PropertyChangedEventArgs e)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
handler(this, e);
}
protected void OnPropertyChanged(string propertyName)
{
OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
}
我在代码上放置了断点,当我按下按钮时,处理程序被调用但为空,然后没有更新或完成,该函数使用新点计算新的 Model3D 对象,但不绘制新结果.
如果我在 public MainViewModel() 部分运行相同的函数,则会显示前 500 个点,但这是由于第一次运行窗口所致。
这是每次按下按钮后模型对象的更新方式。
public void CoreCalculations()
{
for (int i = 0 ; i < 500; i++)
{
//read File Line
// --- Do something with the points ---
// create a mesh using the points and AddRectangularMesh
}
MeshGeometry3D mesh = meshBuilder.ToMesh(true);
Material blueMaterial = MaterialHelper.CreateMaterial(Colors.Blue);
modelGroup.Children.Add(new GeometryModel3D { Geometry = mesh, Transform = new TranslateTransform3D(2, 0, 0), Material = blueMaterial, BackMaterial = blueMaterial });
Model = modelGroup;
}
【问题讨论】:
标签: c# wpf helix-3d-toolkit