在.NET C#应用程序WinForms中使用ActiViz时如何定位x6464位平台

问题描述

我正在创建此问题,并自己回答,以使用.NET Framework(C#)中的ActiViz分享一种运行/调试和构建应用程序(Windows窗体)的简单方法。 It’s OK to Ask and Answer Your Own Questions


如果我创建一个新的Windows Forms应用程序(.NET Framework,C#)并目标为x64平台:

Targeting a x64 platform.

然后我可以转到“项目”>“管理NuGet程序包”并搜索/安装Activiz.NET.x64(v5.8.0)。

但是,在安装Activiz.NET.x64之后,如果我尝试将RenderWindowControl拖到Form上,则会显示以下错误(无法加载工具箱项“ RenderWindowControl”。): / p>

Failed to load toolbox item 'RenderWindowControl'.

是否有解决此问题的方法?

我知道herehere (answer not accepted)已经回答了这个问题;但是,这些答案包括使用Activiz.NET.x86(32位)设计/调试应用程序,并且仅为应用程序的发行版安装Activiz.NET.x64(64位)。在这两个ActiViz软件包之间来回切换显然很麻烦。

解决方法

Kitware ActiViz website的常见问题中,显示以下内容:

ActiViz 64是否可以与Visual Studio一起使用?

Visual Studio是32位 应用程序,因此64位控制不起作用,您需要 在Visual Studio中使用设计器时,ActiViz的32位版本 工作室。通常,使用32位版本进行设计,而使用64位版本 版本用于最终编译。

但是,解决此问题的方法是:

  1. 创建新的Windows Forms应用程序(.NET C#),并立即定位x64(64位)平台;

  2. 通过“项目”>“管理NuGet程序包”安装Activiz.NET.x64(在编写此答案时为v5.8.0);

  3. 向您的Panel添加一个viewportPanel(例如,命名为Form)。这个Panel将是ParentRenderWindowControl的地方;

  4. 在表单的构造函数中创建一个RenderWindowControl实例,如下所示:

    using Kitware.VTK;
    using System.Windows.Forms;
    namespace ActiVizTest
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
                RenderWindowControl renderWindowControl = new RenderWindowControl()
                {
                    Parent = viewportPanel,AddTestActors = true
                };
            }
        }
    }
    

您现在可以在使用Activiz.NET.x64的同时运行/调试并构建应用程序:

enter image description here

但是,仍然可能存在问题:

让我们说我希望背景是红色的。

public Form1()
{
    InitializeComponent();

    RenderWindowControl renderWindowControl = new RenderWindowControl()
    {
        Parent = viewportPanel,AddTestActors = true
    };

    renderWindowControl.RenderWindow.GetRenderers().GetFirstRenderer().SetBackground(1,0);
}

添加上面显示的新代码行将引发System.NullReferenceException。这是因为renWinControl.RenderWindow在初始化null之前是Form1

因此,我们需要在RenderWindow上进行的任何设置都应在表单的构造函数之后完成,例如,我们可以创建一个Load事件。同时,我们还可以创建一些字段以更方便地访问RenderWindowRenderer

完整代码:

using Kitware.VTK;
using System;
using System.Windows.Forms;
namespace ActiVizTest
{
    public partial class Form1 : Form
    {
        private readonly RenderWindowControl renderWindowControl;
        private vtkRenderWindow renderWindow;
        private vtkRenderer renderer;
        public Form1()
        {
            InitializeComponent();

            renderWindowControl = new RenderWindowControl()
            {
                Parent = viewportPanel,AddTestActors = true
            };
        }
        private void Form1_Load(object sender,EventArgs e)
        {
            renderWindow = renderWindowControl.RenderWindow;
            renderer = renderWindow.GetRenderers().GetFirstRenderer();

            renderer.SetBackground(1,0);
        }
    }
}

最后,在x64中调试:

enter image description here

编辑

最好不要使用Form_Load事件,而要使用RenderWindowControl_Load事件。只需记住,在加载控件之前,RenderWindowControl.RenderWindownull

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...