.NET8:快速集成Rapid.NET三维控件

.NET
353
0
0
2024-03-19

.NET8正式版本发布了,AnyCAD Rapid.NET针对.NET8进行了升级和优化。本文以WPF项目为例介绍在.NET8中使用AnyCAD Rapid.NET三维控件。

1 从.NET6升级

若之前使用NET6升级到.NET8,升级过程非常简单,升级到AnyCAD Rapid .NET最新版本后,仅需要更改以下两处:

(1).csproj文件

 <TargetFramework>net8.0-windows</TargetFramework>    

(2)控件窗口

xmlns:anycad="clr-namespace:AnyCAD.WPF;assembly=AnyCAD.WPF.NET8"    

2 在.NET8项目中集成控件

对于新建的.NET8项目,使用AnyCAD Rapid .NET控件仅需要三步:

- 使用nuget安装控件

- 程序初始化

- 在XAML中使用控件

具体过程如下:

(1)安装Rapid .NET控件最新版本

(2)程序初始化

App.xaml

  <Application x:Class="WpfStarter.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:WpfStarter"
             StartupUri="MainWindow.xaml" <strong>Startup="Application_Startup" Exit="Application_Exit"</strong>>
    <Application.Resources>
          
    </Application.Resources>
</Application>

App.xaml.cs

using System.Windows;
 
namespace WpfStarter
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        private void Application_Startup(object sender, StartupEventArgs e)
        {
           <strong> AnyCAD.Foundation.GlobalInstance.Initialize();</strong>
        }
 
        private void Application_Exit(object sender, ExitEventArgs e)
        {
            <strong>AnyCAD.Foundation.GlobalInstance.Destroy();</strong>
        }
    }
}
  

(3) 窗口中添加三维控件

MainWindow.xaml

 <Window x:Class="WpfStarter.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfStarter"
       <strong> xmlns:anycad="clr-namespace:AnyCAD.WPF;assembly=AnyCAD.WPF.NET8"</strong>
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition MinWidth="100" Width="0.3*"/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
      <strong>  <anycad:RenderControl Grid.Column="1" x:Name="mRenderCtrl" Margin="0,0,0,0" ViewerReady="mRenderCtrl_ViewerReady" /></strong>
 
    </Grid>
</Window>

  

MainWindow.xaml.cs

<strong>using AnyCAD.Foundation;</strong>
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows;
 
namespace WpfStarter
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
             
        }
 
        private void mRenderCtrl_ViewerReady()
        {
<strong>            var shape = ShapeBuilder.MakeCylinder(GP.XOY(), 10, 20, 0);
            mRenderCtrl.ShowShape(shape, ColorTable.AliceBlue);</strong>
        }
    }
}

3 编译和运行项目

4 总结

.NET8带来了诸多新特性,建议还在使用.NET Framework 4.x的用户尽快升级。使用.NET6的用户升级到.NET8也是非常的丝滑~