原贴:CAD.Net 从.Net Framework移植到.Net Standard
采用 .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资源文件夹存放…….
更多信息可能需要大家自己实践……..
————————————————————————————————————————————————————————————————————————————————————————————————————————
写.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文件,需要大家认真阅读.
<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop"><PropertyGroup><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>bin\Debug\</OutputPath><RunPostBuildEvent>OnBuildSuccess</RunPostBuildEvent></PropertyGroup><PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Debug|net35|AnyCPU'"><DefineConstants>DEBUG;TRACE</DefineConstants><DefineConstants>Debug;AC2008</DefineConstants><GenerateSerializationAssemblies>Off</GenerateSerializationAssemblies></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>TRACE;DEBUG;Debug;AC2021</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 Assemblies\Microsoft\Framework\.NETFramework\v4.0\System.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 Assemblies\Microsoft\Framework\.NETFramework\v4.5\System.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 Assemblies\Microsoft\Framework\.NETFramework\v4.6\System.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 Assemblies\Microsoft\Framework\.NETFramework\v4.7\System.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 Assemblies\Microsoft\Framework\.NETFramework\v4.8\System.Xaml.dll</HintPath></Reference><!--WPF需要的新特性--><PackageReference Include="System.Runtime" Version="4.0.0" /></ItemGroup><ItemGroup><PackageReference Include="iTextSharp" Version="5.5.13.1" /><PackageReference Include="Microsoft.QualityTools.Testing.Fakes" Version="16.7.4-beta.20330.2" /></ItemGroup><!--WPF的包--><ItemGroup><PackageReference Include="MvvmLightLibs" Version="5.4.1.1" /><PackageReference Include="Newtonsoft.Json" Version="12.0.3" /></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 Update="View\MainView.xaml.cs"><DependentUpon>MainView.xaml</DependentUpon></Compile></ItemGroup><ItemGroup><Page Update="View\MainView.xaml"><SubType>Designer</SubType></Page></ItemGroup></Project>
ps:这里有个问题cad2008和cad2010会同时占用net3.5版本,所以我的操作是不用cad2010
2010可以单独配置一个工程来实现….笑
图文并茂开始了:
(以下是小贱贱总结的…我会加一点注释上去,方便大家理解).
双击这个位置,就可以编辑.csproj文件哟~
粘贴我本篇文章的.csproj…
要注意的事项



注明:找nuget包之后点安装,实际上来说是一个十分不明智的操作,因为这样安装会装在所有的net版本上面,
也就是它会自动写在.csproj最下面的公共部分,而当你需要写在特定位置的时候可以利用nuget包名字写在.csproj的net版本下面.

别名方便区分cad的版本的话,可以按如下的方式进行 
调试的时候如果没有命中断点,请修改这个位置.
如果没有这个左边的配置文件,要对着工程右键-属性,弹出来,修改……
