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

标签 c# wpf xaml nullreferenceexception vtk

我正在尝试使用 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/

相关文章:

c# - 最简单的 C# 开发免费设置

c# - 基于不同XAML的样式继承

c# - 多线程任务更新 1 个进度条 - UI C# WPF

xaml - 无法使用 Telerik.UI.Xaml.Controls.Grid 命名空间添加

c# - 你能在类型数组上编写扩展方法吗?

c# - 如何将依赖项注入(inject) Caliburn.Micro 中的 View 模型?

c# - 无法使用c#将json结果反序列化为自定义列表对象

c# - WPF + 符号在 xaml 中做什么

xaml - 条件 XAML 崩溃 UWP 应用程序

c# - 是否可以强制基于DependencyProperty的绑定(bind)以编程方式重新评估?