我是 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>
下一個重要部分是 MainWindow 的 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。在用戶界面中,我有兩個按鈕向前和向后。前進按鈕試圖從 txt 檔案中繪制接下來的 500 個點。
在我的 MainWindow.xaml.cs 檔案中,我呼叫按鈕的事件處理程式,這些按鈕呼叫 MainViewModel.cs 檔案中的函式,其中計算了該資料點的所有函式,最終結果將保存在 Model3D 模型物件上,然后必須繪制在 UI 上。
按鈕呼叫 Forward_click 事件并增加計數器以讀取下一個資料點
<Button Margin="5" Click="Forward_Click">Forward</Button>
After I press the button the Event Handlers is called but it is null, so the UI is never update and I can't show the next data points. In my code I have implemented the Event handler, to listen on the changes of the Model, so after the execution of the functions the model object will be updated and then call the handler to update the UI.
The MainViewModel Class is defined as:
public class MainViewModel : Window, INotifyPropertyChanged
and the event handlers as below:
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));
}
I put breakpoints on the code, and when I press the Button, the handler is called but is null and then nothing is updated or done, the function calculates the new Model3D object with new points, but does not draw the new result.
If I run the same function on public MainViewModel() section the first 500 points are shown, but it due to the first run of the window.
This is how the Model object is updated after each button press.
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;
}
uj5u.com熱心網友回復:
嘗試更改OneWayToSource為OneWay. 從檔案
當目標屬性更改時更新源屬性。
你可能想反過來做。每當模型更改時更新目標。
MainViewModel繼承自有點奇怪Window。我建議看一下 helix3D“SimpleDemo”。只是為了確認更改模型有效,使 mainViewModel 繼承 INotifyPropertyChanged,并添加以下代碼:
private async Task RemoveModel()
{
await Task.Delay(5000);
Model = null;
OnPropertyChanged(nameof(Model));
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
并呼叫RemoveModel建構式。這應該使模型在 5 秒后被移除。請注意,示例代碼適用于除錯,僅此而已。
您還應該能夠使用實時可視化樹資源管理器來幫助除錯 UI。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/315353.html
標籤:c# wpf helix-3d-工具包
上一篇:在計時器上獲取經過的時間
