繁体   English   中英

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

[英]How to target x64 (64 bit) platform while using ActiViz in a .NET C# application (WinForms)

我正在创建这个问题并自己回答它,以分享一种在 .NET Framework (C#) 中使用 ActiViz 来运行/调试和构建应用程序(Windows 窗体)的简单方法。 可以提出和回答您自己的问题


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

针对 x64 平台。

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

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

无法加载工具箱项“RenderWindowControl”。

这个问题有解决方法吗?

我知道这里这里已经回答这个问题(答案不被接受) 但是,这些答案包括使用 Activiz.NET.x86(32 位)设计/调试应用程序,并且只为应用程序的发布版本安装 Activiz.NET.x64(64 位)。 在两个 ActiViz 软件包之间来回切换显然非常麻烦。

Kitware ActiViz 网站的常见问题解答中,提供了以下内容:

ActiViz 64 是否适用于 Visual Studio?

Visual Studio 是 32 位应用程序,因此 64 位控件不起作用,并且在 Visual Studio 中使用设计器时需要 32 位版本的 ActiViz。 通常,32 位版本用于设计,64 位版本用于最终编译。

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

  1. 创建新的 Windows 窗体应用程序 (.NET C#) 并立即面向 x64(64 位)平台;

  2. 通过 Project > Manage NuGet Packages 安装 Activiz.NET.x64(在撰写本答案时为 v5.8.0);

  3. Panel (例如名为viewportPanel )添加到您的Form Panel将是RenderWindowControlParent

  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 的同时运行/调试和构建应用程序:

在此处输入图片说明

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

假设我希望背景为红色。

public Form1()
{
    InitializeComponent();

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

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

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

所以,我们需要在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, 0);
        }
    }
}

最后,在 x64 中调试:

在此处输入图片说明

编辑

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM