简体   繁体   English

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

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

I am creating this question and answering it myself to share an easy way to run/debug and build an application (Windows Forms), using ActiViz in the .NET Framework (C#).我正在创建这个问题并自己回答它,以分享一种在 .NET Framework (C#) 中使用 ActiViz 来运行/调试和构建应用程序(Windows 窗体)的简单方法。 It's OK to Ask and Answer Your Own Questions . 可以提出和回答您自己的问题


If I create a new Windows Forms application (.NET Framework, C#) and target x64 platforms:如果我创建一个新的 Windows 窗体应用程序(.NET Framework、C#)并以 x64 平台为目标:

针对 x64 平台。

I can then go to Project > Manage NuGet Packages and search/install Activiz.NET.x64 (v5.8.0).然后,我可以转到“项目”>“管理 NuGet 包”并搜索/安装 Activiz.NET.x64 (v5.8.0)。

However, after installing Activiz.NET.x64, if I try to drag the RenderWindowControl to a Form , the following error is shown (Failed to load toolbox item 'RenderWindowControl'.):但是,在安装 Activiz.NET.x64 后,如果我尝试将RenderWindowControl拖动到Form ,则会显示以下错误(无法加载工具箱项目“RenderWindowControl”。):

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

Is there a workaround for this issue?这个问题有解决方法吗?

I know this question has been answered here and here (answer not accepted) ;我知道这里这里已经回答这个问题(答案不被接受) however, these answers consist in designing/debugging the application with Activiz.NET.x86 (32 bit) and only installing Activiz.NET.x64 (64 bit) for the release version of the application.但是,这些答案包括使用 Activiz.NET.x86(32 位)设计/调试应用程序,并且只为应用程序的发布版本安装 Activiz.NET.x64(64 位)。 It is obviously very cumbersome to switch back and forth between both ActiViz packages.在两个 ActiViz 软件包之间来回切换显然非常麻烦。

In the FAQ of the Kitware ActiViz website the following is presented:Kitware ActiViz 网站的常见问题解答中,提供了以下内容:

Does ActiViz 64 work with Visual Studio? ActiViz 64 是否适用于 Visual Studio?

Visual Studio is a 32 bits application, therefore 64 bits control does not work and you need the 32 bits version of ActiViz when using the designer within Visual Studio. Visual Studio 是 32 位应用程序,因此 64 位控件不起作用,并且在 Visual Studio 中使用设计器时需要 32 位版本的 ActiViz。 Usually, the 32 bits version is used to design and the 64 bits version is used for the final compilation.通常,32 位版本用于设计,64 位版本用于最终编译。

However, a workaround for this issue is to:但是,此问题的解决方法是:

  1. Create your new Windows Forms application (.NET C#) and immediately target x64 (64 bit) platforms;创建新的 Windows 窗体应用程序 (.NET C#) 并立即面向 x64(64 位)平台;

  2. Install Activiz.NET.x64 (v5.8.0 at the time of writing this answer) through Project > Manage NuGet Packages;通过 Project > Manage NuGet Packages 安装 Activiz.NET.x64(在撰写本答案时为 v5.8.0);

  3. Add a Panel (named viewportPanel , for example) to your Form .Panel (例如名为viewportPanel )添加到您的Form This Panel will be the Parent to a RenderWindowControl ;Panel将是RenderWindowControlParent

  4. Create a RenderWindowControl instance in the form's constructor like so:在窗体的构造函数中创建一个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 }; } } }

You can now run/debug and build the application while using Activiz.NET.x64:您现在可以在使用 Activiz.NET.x64 的同时运行/调试和构建应用程序:

在此处输入图片说明

However, there is still a possible issue:但是,仍然存在一个可能的问题:

Lets say that I want the background to be red.假设我希望背景为红色。

public Form1()
{
    InitializeComponent();

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

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

Adding the new line of code shown above will raise a System.NullReferenceException .添加上面显示的新代码行将引发System.NullReferenceException This is because renWinControl.RenderWindow is null before Form1 is initialized.这是因为在Form1初始化之前renWinControl.RenderWindownull

So, any setup we need to do on the RenderWindow should be done after the form's constructor, for example, we can create a Load event.所以,我们需要在RenderWindow上做的任何设置都应该在窗体的构造函数之后完成,例如,我们可以创建一个Load事件。 And while we are at it, we can also create some fields to have easier access to the RenderWindow and Renderer .当我们在做的时候,我们还可以创建一些字段来更容易地访问RenderWindowRenderer

Full code:完整代码:

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);
        }
    }
}

And finally, debugging in x64:最后,在 x64 中调试:

在此处输入图片说明

EDIT编辑

Instead of using the Form_Load event, it is best to use the RenderWindowControl_Load event.最好使用RenderWindowControl_Load事件而不是使用Form_Load事件。 Just remember that RenderWindowControl.RenderWindow is null before the control is loaded.请记住,在加载控件之前RenderWindowControl.RenderWindownull

暂无
暂无

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

相关问题 在64位计算机上基于AnyCPU vs x64平台构建的C#应用​​程序的性能 - Performance of a C# application built on AnyCPU vs x64 platform on a 64 bit machine 使用64位dll时C#.Net X64应用程序崩溃 - C#.Net X64 Application crash when using 64bit dll 如何从 c# 中的 x86 应用程序启动 x64 位应用程序 - How to start x64 bit application from x86 application in c# c#visual studio使用目标anycpu构建exe,但在调用进程平台(x86 / x64)上确定其平台(x86 / x64) - c# visual studio build exe with target anycpu but that determines its platform(x86/x64) on the calling process platform(x86/x64) / platform开关对x64窗口中C#汇编的影响 - Effects of /platform switch on C# assembly in x64 windows 生成英特尔兼容的x64位dll(C#) - generate Intel compatible x64 bit dll (C#) 在运行 Windows 10 Professional x64 的移动设备中使用内置 FlashLight 的 C# WinForms - C# WinForms using FlashLight built-in into mobile device running Windows 10 Professional x64 Visual Studio C# 目标平台 x86 但 nuget package 在我看来是 x64 - 程序运行时出错 - Visual Studio C# target platform x86 but nuget package seems to me x64 - error when program runs 在项目设置为x86时,在C#中启动x64 Windows应用程序 - Launch x64 Windows application in C# while the project is set to x86 平台目标 (x86/x64/AnyCPU) 对 .net 卫星程序集有影响吗 - Does Platform Target (x86/x64/AnyCPU) matter for.net satellite assemblies
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM