cad.net 从.net framework移植到.net standard

说明

首先要说明的是.net standard是为了生成不同net版本的dll而存在的.

若你想要生成不同版本的exe,怕是需要.net5的技术.

移植需求:

采用 .net standard 类库和 nuget 的方式的好处是:

  • .net的发展的趋势就是干掉 frameworks 类型,只保留 standard(core) 类型。frameworks在4.8版本之后就不在升级了,以后都会统一到.net 5。
    因此现在采用standard类型是一个正确的选择。
  • 经过测试,cad是支持standard类型的类库加载和运行的,因此建议发现不可修复的bug之前,针对cad的二次开发也采用standard类型的类库。
  • 采用nuget管理引用是可以不用管本地是否有要引用的dll文件,同时nuget安装的类库还会自动升级,方便采用更新的类库。

以上摘录自: https://gitee.com/vicwjb/NFox

因此,我将工程移植到 .net standard....

而很幸福的是,不需要改代码,只需要学习写.csporj文件就可以了...这相当于写一个xml吧...

ps: 测试了之后,发现同名变量域的操作需要改一下代码...

      WinForm的资源文件要重新关联一下,因为 standard 要有一个单独的 Resources 资源文件夹存放...

更多信息可能需要大家自己实践...

快速移植:

1: 复制粘贴我的.csproj文件到你工程的.csproj文件

2: 删除项目Properties文件夹中冲突的部分,一般来说我是全删的...如果你有做其他工作的话,请保证备份.

界面支持:

 (完整的.csproj文件在下面提供) 

写.csproj文件,支持WinFrom和WPF:

我在移植的时候发现了一个很重要的问题 .net framework 环境下面的 WPF和winform 窗体设计器 InitializeComponent(); 这个语句会不断发生无法检索的问题,

因为这文件是vs自己生成的,不可以通过修改路径的方法将它呈现给编译器,在实现了很多个不同的方法之后,发现了,有的只支持WinFrom有的只支持WPF.

而 .net standard 上面也会遇到这个问题,这是移植要解决的第一个问题.

  

 .csproj 文件的第一句:

如果你是个萌新,那么直接新建的 .net standard 工程,那么它是只支持WinForm的

<Project Sdk="Microsoft.NET.Sdk">

 如果你找到了这一句,替代进去,那么它只支持了..WPF....

<Project Sdk="MSBuild.Sdk.Extras/2.0.54">

如果你看到了我的博文,那么这一句,终于同时支持了这两个鬼东西了...原因是net core3.1的推出...

<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">

版本和引用处理:

WPF的MarkupExtension接口引用问题,System.Xaml.dll的引用问题:

WPF 存在一个版本差,这个版本差分别是低版本的 net3.5 和 高版本的 net4.0,

而WPF的 MarkupExtension 接口在 net3.5 和 net4.0 之间有差异,这个差异需要在 net4.0(或以上) 引用一个 System.Xaml.dll 来解决. 

在.csporj文件这里写上引用:

但是因为要判断 net 的版本,所以需要写在这里:

  

文件参考:

我完整的.csproj文件:

ps:这里有个问题cad2008和cad2010会同时占用net3.5版本,所以我的操作是不用cad2010

2010可以单独配置一个工程来实现....(笑)

我的自制的dll包已经上传到了微软的服务器,如果缺少的部分是感叹号,去删掉对应的位置就好了.

<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">   
    <PropertyGroup>
      <!--永远支持最新语法 preview,默认是latestMajor-->
      <LangVersion>preview</LangVersion>
      <!--版本号迭代-->
      <AssemblyVersion>1.0.0.*</AssemblyVersion>
      <FileVersion>1.0.0.0</FileVersion>
      <Deterministic>False</Deterministic>

      <TargetFrameworks>NET35;NET40;NET45;NET46;NET47;NET48</TargetFrameworks>
      <!-- 支持wpf -->
      <UseWpf>true</UseWpf>
      <!-- 支持winform -->
      <UseWindowsForms>true</UseWindowsForms>
      <!-- 以下是默认引用相关依赖的属性 WPF -->
      <ExtrasEnableWpfProjectSetup>true</ExtrasEnableWpfProjectSetup>
      <!-- 以下是默认引用相关依赖的属性 WindowsForms -->
      <ExtrasEnableWinFormsProjectSetup>true</ExtrasEnableWinFormsProjectSetup>
      <!--输出路径-->
      <OutputPath>....K01.惊惊连盒</OutputPath>
      <RunPostBuildEvent>OnBuildSuccess</RunPostBuildEvent>
    </PropertyGroup>

    <PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Debug|net35|AnyCPU'">
        <DefineConstants>DEBUG;TRACE</DefineConstants>
        <DefineConstants>Debug;AC2008</DefineConstants>
    </PropertyGroup>

    <PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Debug|AC2010|AnyCPU'">
        <DefineConstants>DEBUG;TRACE</DefineConstants>
        <DefineConstants>$(Configuration);AC2010</DefineConstants>
    </PropertyGroup>

    <PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Debug|net40|AnyCPU'">
        <DefineConstants>DEBUG;TRACE</DefineConstants>
        <DefineConstants>$(Configuration);AC2013</DefineConstants>
    </PropertyGroup>

    <PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Debug|net45|AnyCPU'">
        <DefineConstants>DEBUG;TRACE</DefineConstants>
        <DefineConstants>$(Configuration);AC2015</DefineConstants>
    </PropertyGroup>

    <PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Debug|net46|AnyCPU'">
        <DefineConstants>DEBUG;TRACE</DefineConstants>
        <DefineConstants>$(Configuration);AC2017</DefineConstants>
    </PropertyGroup>

    <PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Debug|net47|AnyCPU'">
        <DefineConstants>DEBUG;TRACE</DefineConstants>
        <DefineConstants>$(Configuration);AC2019</DefineConstants>
    </PropertyGroup>

    <PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Debug|net48|AnyCPU'">
        <DefineConstants>DEBUG;TRACE</DefineConstants>
        <DefineConstants>$(Configuration);AC2020</DefineConstants>
    </PropertyGroup>




    <PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Release|NET35|AnyCPU'">
        <DefineConstants>TRACE;AC2008</DefineConstants>
    </PropertyGroup>

    <PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Release|AC2010|AnyCPU'">
        <DefineConstants>TRACE;AC2010</DefineConstants>
    </PropertyGroup>

    <PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Release|net40|AnyCPU'">
        <DefineConstants>TRACE;AC2013</DefineConstants>
    </PropertyGroup>

    <PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Release|net45|AnyCPU'">
        <DefineConstants>TRACE;AC2015</DefineConstants>
    </PropertyGroup>

    <PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Release|net46|AnyCPU'">
        <DefineConstants>TRACE;AC2017</DefineConstants>
    </PropertyGroup>

    <PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Release|net47|AnyCPU'">
        <DefineConstants>TRACE;AC2019</DefineConstants>
    </PropertyGroup>

    <PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Release|net48|AnyCPU'">
        <DefineConstants>TRACE;AC2020</DefineConstants>
    </PropertyGroup>


    <ItemGroup Condition="'$(TargetFramework)' == 'net35'">
        <!--ACAD的dll,因为Acad2008不可以复制dll到目标,否则会无法使用命令-->
        <PackageReference Include="AutoCad.Net.2008-JingBox" Version="1.0.0">
            <ExcludeAssets>runtime</ExcludeAssets>
        </PackageReference>
        <!--WPF的dll-->
        <PackageReference Include="CommonServiceLocator" Version="1.0.0" />
    </ItemGroup>

    <ItemGroup Condition="'$(TargetFramework)' == 'net351'">
        <!--ACAD的dll,因为Acad2008不可以复制dll到目标,否则会无法使用命令-->
        <PackageReference Include="AutoCad.Net.2010-JingBox" Version="1.0.0">
            <ExcludeAssets>runtime</ExcludeAssets>
        </PackageReference>
        <!--WPF的dll-->
        <PackageReference Include="CommonServiceLocator" Version="1.0.0" />
    </ItemGroup>

    <ItemGroup Condition="'$(TargetFramework)' == 'net40'">
        <!--ACAD的dll-->
        <PackageReference Include="AutoCad.Net.2013-JingBox" Version="1.0.0" />
        <!--WPF的dll-->
        <PackageReference Include="CommonServiceLocator" Version="2.0.2" />
        <!--WPF需要的dll,System.Xaml在net3.5没有-->
        <Reference Include="System.Xaml">
            <HintPath>C:Program Files (x86)Reference AssembliesMicrosoftFramework.NETFrameworkv4.0System.Xaml.dll</HintPath>
        </Reference>
    </ItemGroup>


    <ItemGroup Condition="'$(TargetFramework)' == 'net45'">
        <!--ACAD的dll-->
        <PackageReference Include="AutoCad.Net.2015-JingBox" Version="1.0.0" />

        <!--WPF的dll-->
        <PackageReference Include="CommonServiceLocator" Version="2.0.2" />

        <!--WPF需要的dll,System.Xaml在net3.5没有-->
        <Reference Include="System.Xaml">
            <HintPath>C:Program Files (x86)Reference AssembliesMicrosoftFramework.NETFrameworkv4.5System.Xaml.dll</HintPath>
        </Reference>
        <!--WPF需要的新特性-->
        <PackageReference Include="System.Runtime" Version="4.0.0" />

    </ItemGroup>

    <ItemGroup Condition="'$(TargetFramework)' == 'net46'">
        <!--ACAD的dll-->
        <PackageReference Include="AutoCad.Net.2017-JingBox" Version="1.0.0" />

        <!--WPF的dll-->
        <PackageReference Include="CommonServiceLocator" Version="2.0.2" />

        <!--WPF需要的dll,System.Xaml在net3.5没有-->
        <Reference Include="System.Xaml">
            <HintPath>C:Program Files (x86)Reference AssembliesMicrosoftFramework.NETFrameworkv4.6System.Xaml.dll</HintPath>
        </Reference>
        <!--WPF需要的新特性-->
        <PackageReference Include="System.Runtime" Version="4.0.0" />
    </ItemGroup>

    <ItemGroup Condition="'$(TargetFramework)' == 'net47'">
        <!--ACAD的dll-->
        <PackageReference Include="AutoCad.Net.2019-JingBox" Version="1.0.0" />

        <!--WPF的dll-->
        <PackageReference Include="CommonServiceLocator" Version="2.0.2" />

        <!--WPF需要的dll,System.Xaml在net3.5没有-->
        <Reference Include="System.Xaml">
            <HintPath>C:Program Files (x86)Reference AssembliesMicrosoftFramework.NETFrameworkv4.7System.Xaml.dll</HintPath>
        </Reference>
        <!--WPF需要的新特性-->
        <PackageReference Include="System.Runtime" Version="4.0.0" />
    </ItemGroup>
    
    <ItemGroup Condition="'$(TargetFramework)' == 'net48'">
        <!--ACAD的dll-->
        <PackageReference Include="AutoCad.Net.2021-JingBox" Version="1.0.0" />

        <!--WPF的dll-->
        <PackageReference Include="CommonServiceLocator" Version="2.0.2" />

        <!--WPF需要的dll,System.Xaml在net3.5没有-->
        <Reference Include="System.Xaml">
            <HintPath>C:Program Files (x86)Reference AssembliesMicrosoftFramework.NETFrameworkv4.8System.Xaml.dll</HintPath>
        </Reference>
        <!--WPF需要的新特性-->
        <PackageReference Include="System.Runtime" Version="4.0.0" />
    </ItemGroup>

    <!--WPF的包-->
    <ItemGroup>
        <PackageReference Include="MvvmLightLibs" Version="5.4.1.1" />
        <PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
    </ItemGroup>
    
    <!--可能需要的包-->
    <ItemGroup>
        <PackageReference Include="iTextSharp" Version="5.5.13.1" />
        <PackageReference Include="Microsoft.QualityTools.Testing.Fakes" Version="16.7.4-beta.20330.2" />
    </ItemGroup>
    
    <ItemGroup>
        <Reference Include="System.Management" />
        <Reference Include="System.ServiceModel" />
        <Reference Include="System.Windows.Forms" />
        <Reference Include="System.Xml" />
        <Reference Include="PresentationFramework" />
    </ItemGroup>
    
    <!--WPF的程序集引用-->
    <ItemGroup>
        <!--因为net3.5没有所以不能统一加System.Xaml,只能去每个net环境下加-->
        <Reference Include="PresentationCore" />
        <Reference Include="PresentationFramework" />
        <Reference Include="WindowsBase" />
        <Reference Include="WindowsFormsIntegration" />
    </ItemGroup>
 
    <!--排除引用的包文件目录-->
    <ItemGroup>
        <Compile Remove="FakesAssemblies**" />
        <EmbeddedResource Remove="FakesAssemblies**" />
        <None Remove="FakesAssemblies**" />
        <Page Remove="FakesAssemblies**" />
    </ItemGroup>

    <ItemGroup>
        <AdditionalDesignTimeBuildInput Remove="FakesAssemblies**" />
    </ItemGroup>

    <ItemGroup>
        <Compile Update="PropertiesSettings.Designer.cs">
            <DesignTimeSharedInput>True</DesignTimeSharedInput>
            <AutoGen>True</AutoGen>
            <DependentUpon>Settings.settings</DependentUpon>
        </Compile>
    </ItemGroup>

    <ItemGroup>
        <None Update="PropertiesSettings.settings">
            <Generator>SettingsSingleFileGenerator</Generator>
            <LastGenOutput>Settings.Designer.cs</LastGenOutput>
        </None>
    </ItemGroup>

</Project>
View Code

图文教程:

图文并茂开始了: 

(以下是小贱贱总结的...我会加一点注释上去,方便大家理解).

   

双击这个位置,就可以编辑.csproj文件哟~

粘贴我本篇文章的.csproj..

要注意的事项

  

 

 

  注明:找nuget包之后点安装,实际上来说是一个十分不明智的操作,因为这样安装会装在所有的net版本上面,

          也就是它会自动写在.csproj最下面的公共部分,而当你需要写在特定位置的时候可以利用nuget包名字写在.csproj的net版本下面.

 

  

 别名方便区分cad的版本的话,可以按如下的方式进行 

 

     

调试

调试的时候如果没有命中断点,请修改这个位置.

   

9.0新语法

20210224支持c#9.0语法,在.csproj文件上面加上:

    <PropertyGroup>
<LangVersion>9.0</LangVersion>
<PropertyGroup>

 但是只支持9.0那到10.0又要改,所以最好是用以下的,支持预览版:

    <PropertyGroup>
        <!--永远支持最新语法 preview,默认是latestMajor-->
        <LangVersion>preview</LangVersion>
    </PropertyGroup>

 这些我已经修改在了上面的完整文件上了,若看到可以忽略.

 记得更新vs,才能使用新语法,新语法参考 https://devblogs.microsoft.com/dotnet/c-9-0-on-the-record/

(完)

原文地址:https://www.cnblogs.com/JJBox/p/12677496.html