[英]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 平台为目标:
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”。):
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:但是,此问题的解决方法是:
Create your new Windows Forms application (.NET C#) and immediately target x64 (64 bit) platforms;创建新的 Windows 窗体应用程序 (.NET C#) 并立即面向 x64(64 位)平台;
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);
Add a Panel
(named viewportPanel
, for example) to your Form
.将
Panel
(例如名为viewportPanel
)添加到您的Form
。 This Panel
will be the Parent
to a RenderWindowControl
;此
Panel
将是RenderWindowControl
的Parent
;
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.RenderWindow
为null
。
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
.当我们在做的时候,我们还可以创建一些字段来更容易地访问
RenderWindow
和Renderer
。
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.RenderWindow
为null
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.