簡體   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