gpt4 book ai didi

c# - NullReferenceException 在 WPF 中为 vtk 加载 RenderWindowControl

转载 作者:太空狗 更新时间:2023-10-29 23:07:04 25 4
gpt4 key购买 nike

我正在尝试使用 ActiViz.NET 和 Visual Studio 2013 从我的 WPF proyect 上的 vtk 库加载 RenderWindowControl。该库工作正常,因为我做了一个新项目只是为了练习它但是当我试图将它集成到我的工作中时,我这次得到了一个空的 RenderWindowControl。这是我的代码:

主窗口.xaml:

<Window x:Class="myProject.Views.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:VtkTab="clr-namespace:myProject.Views.UITabs.VtkTab"
x:Name="Mainwindow"
MinHeight="600"
MinWidth="800"
Title="{Binding Title}"
Height="720"
Width="1280"
Icon="{StaticResource ApplicationIcon}"
Loaded="OnLoaded"
DataContext="{Binding Main, Source={StaticResource ViewModelLocator}}"
Style="{StaticResource WindowStyle}"
mc:Ignorable="d">

<DockPanel>
<TabControl>
....
....
<VtkTab:VtkTabView />
....
....
</TabControl>
</DockPanel>
</Window>

VtkTabView.xaml:

<UserControl x:Class="myProject.Views.UITabs.VtkTab.VtkTabView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vtk="clr-namespace:Kitware.VTK;assembly=Kitware.VTK"
Loaded="WindowLoaded"
Height="480" Width="640">
<WindowsFormsHost Name="Wfh">
<vtk:RenderWindowControl x:Name="RenderControl" />
</WindowsFormsHost>
</UserControl>

VtkTabView.xaml.cs:

public partial class UITabView
{

protected static Random _random = new Random();
vtkActor actor = vtkActor.New();

public VtkTabView()
{
InitializeComponent();

var sphere = vtkSphereSource.New();
sphere.SetThetaResolution(8);
sphere.SetPhiResolution(16);

var shrink = vtkShrinkPolyData.New();
shrink.SetInputConnection(sphere.GetOutputPort());
shrink.SetShrinkFactor(0.9);

var move = vtkTransform.New();
move.Translate(_random.NextDouble(), _random.NextDouble(), _random.NextDouble());
var moveFilter = vtkTransformPolyDataFilter.New();
moveFilter.SetTransform(move);

moveFilter.SetInputConnection(shrink.GetOutputPort());

var mapper = vtkPolyDataMapper.New();
mapper.SetInputConnection(moveFilter.GetOutputPort());

// The actor links the data pipeline to the rendering subsystem
actor.SetMapper(mapper);
actor.GetProperty().SetColor(1, 0, 0);
}

private void WindowLoaded(object sender, RoutedEventArgs e)
{
var renderer = RenderControl.RenderWindow.GetRenderers().GetFirstRenderer();
renderer.AddActor(actor);
}
}

RenderControl.RenderWindow 在 WindowLoaded (VtkTabView.xaml.cs) 上为空,我不知道为什么。可能是因为我从第二个 xamp 加载 UITabView 而我丢失了 RenderControl 的内容?这是我看到的与我所做的示例相比的唯一区别。

最佳答案

访问 RenderWindowControl 的 RenderWindow on Load 事件。

例如

public VtkTabView()
{
InitializeComponent();
// initialize your sphrere and actor

RenderControl.Load += MyRenderWindowControlOnLoad;
}

private void MyRenderWindowControlOnLoad(object sender_in, EventArgs eventArgs_in){

//access the RenderWindow here

}

关于c# - NullReferenceException 在 WPF 中为 vtk 加载 RenderWindowControl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27290044/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com