[英]XAML automatic code generation not working after editing .csproj file
My project was originally an old .NET Framework 4.5 project and I was trying to migrate to 4.6.1 and add a reference to a .NET Standard 2 library. 我的项目最初是一个旧的.NET Framework 4.5项目,而我试图迁移到4.6.1并添加对.NET Standard 2库的引用。 So I edited the csproj
file and replaced its contents with the following: 因此,我编辑了csproj
文件,并将其内容替换为以下内容:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net461</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Extended.Wpf.Toolkit" Version="3.4.0" />
<PackageReference Include="FFME.Windows" Version="4.0.270" />
<PackageReference Include="FontAwesome5" Version="1.0.7" />
<PackageReference Include="SevenZipSharp.Net45" Version="1.0.19" />
<PackageReference Include="WindowsAPICodePack" Version="1.1.0" />
</ItemGroup>
<ItemGroup>
</ItemGroup>
<ItemGroup>
<Reference Include="PresentationCore" />
<Reference Include="PresentationFramework" />
<Reference Include="System.ComponentModel.DataAnnotations" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="System.Net" />
<Reference Include="System.Windows" />
<Reference Include="System.Windows.Forms" />
<Reference Include="System.Windows.Presentation" />
<Reference Include="WindowsBase" />
</ItemGroup>
</Project>
I have used this method in the past to migrate non-WPF Windows-based projects to have new project structure and it works as long as WPF is not used; 过去,我曾使用此方法将基于Windows的非WPF项目迁移到新的项目结构,只要不使用WPF,它就可以工作。 but wherever XAML
is involved, the auto-generated partial classes are missing. 但是无论涉及XAML
哪里,都会丢失自动生成的部分类。 How can I get this to work? 我该如何工作?
You lost some important properties and some important items in your csproj file. 您在csproj文件中丢失了一些重要的属性和一些重要的项目。
You should add a LanguageTargets
property, and if you lost it WPF XAML files will not build. 您应该添加LanguageTargets
属性,如果丢失了该属性,则不会生成WPF XAML文件。
Then copy the items below to include all your XAML files, resource files and setting files. 然后复制以下项目,以包括所有XAML文件,资源文件和设置文件。
It's recommended to copy the code below to your csproj file and add your own other items. 建议将下面的代码复制到您的csproj文件中,并添加自己的其他项目。
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net47</TargetFramework>
<!-- Important! You've lost it. -->
<LanguageTargets>$(MSBuildToolsPath)\Microsoft.CSharp.targets</LanguageTargets>
<!-- If this is not the main exe project, Remove it. -->
<OutputType>WinExe</OutputType>
</PropertyGroup>
<ItemGroup>
<!-- App.xaml -->
<ApplicationDefinition Include="App.xaml" SubType="Designer" Generator="MSBuild:Compile" />
<!-- XAML elements -->
<Page Include="**\*.xaml" Exclude="App.xaml" SubType="Designer" Generator="MSBuild:Compile" />
<Compile Update="**\*.xaml.cs" DependentUpon="%(Filename)" />
<!-- Resources -->
<EmbeddedResource Update="Properties\Resources.resx" Generator="ResXFileCodeGenerator" LastGenOutput="Resources.Designer.cs" />
<Compile Update="Properties\Resources.Designer.cs" AutoGen="True" DependentUpon="Resources.resx" DesignTime="True" />
<!-- Settings -->
<None Update="Properties\Settings.settings" Generator="SettingsSingleFileGenerator" LastGenOutput="Settings.Designer.cs" />
<Compile Update="Properties\Settings.Designer.cs" AutoGen="True" DependentUpon="Settings.settings" />
</ItemGroup>
<ItemGroup>
<Reference Include="PresentationCore" />
<Reference Include="PresentationFramework" />
<Reference Include="System.Xaml" />
<Reference Include="WindowsBase" />
</ItemGroup>
</Project>
Your csproj file will be much simpler. 您的csproj文件将更加简单。
<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
<PropertyGroup>
<TargetFramework>netcoreapp3.0</TargetFramework>
<UseWPF>true</UseWPF>
<!-- If this is not the main exe project, Remove it. -->
<OutputType>WinExe</OutputType>
</PropertyGroup>
</Project>
You may want to add more application properties in your <PropertyGroup />
: 您可能想在<PropertyGroup />
添加更多应用程序属性:
<PropertyGroup>
<!-- If you have App.manifest, add this here. -->
<!-- <ApplicationManifest>Properties\App.manifest</ApplicationManifest> -->
<!-- f you have App.ico, add this here. -->
<!-- <ApplicationIcon>Properties\App.ico</ApplicationIcon> -->
<!-- If you have another startup object instead of App.xaml, change it to it. -->
<StartupObject />
</PropertyGroup>
The csproj file content above are posted on my sites: 上面的csproj文件内容发布在我的网站上:
The problem is with not including Page
tags from the old .csproj
file. 问题在于不包括旧.csproj
文件中的Page
标记。 This is how the new .csproj
file looks like: 这是新的.csproj
文件的样子:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net461</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Extended.Wpf.Toolkit" Version="3.4.0" />
<PackageReference Include="FFME.Windows" Version="4.0.270" />
<PackageReference Include="FontAwesome5" Version="1.0.7" />
<PackageReference Include="SevenZipSharp.Net45" Version="1.0.19" />
<PackageReference Include="WindowsAPICodePack" Version="1.1.0" />
</ItemGroup>
<ItemGroup>
</ItemGroup>
<ItemGroup>
<Reference Include="PresentationCore" />
<Reference Include="PresentationFramework" />
<Reference Include="System.ComponentModel.DataAnnotations" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="System.Net" />
<Reference Include="System.Windows" />
<Reference Include="System.Windows.Forms" />
<Reference Include="System.Windows.Presentation" />
<Reference Include="WindowsBase" />
</ItemGroup>
<ItemGroup>
<Page Include="MainWindow.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
...
</ItemGroup>
</Project>
Now it's working like a charm! 现在,它就像一种魅力一样运作!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.