简体   繁体   English

XAML自动代码生成在编辑.csproj文件后无法正常工作

[英]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文件中丢失了一些重要的属性和一些重要的项目。

If you target .NET Framework: 如果目标是.NET Framework:

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>

If you target WPF on .NET Core: 如果在.NET Core上定位WPF:

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>

Tips 提示

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>

Posts 帖子

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.

相关问题 T4代码生成摘要(不自动编译,不自动文件管理) - T4 Code Generation In the Abstract (No Automatic Compilation, No Automatic File Management) 自动生成 Resource.resx 的翻译文件不起作用 - Automatic generation of translation file for Resource.resx is not working 用于项目文件编辑的Visual Studio加载项(csproj) - Add-in for Visual Studio for project file editing (csproj) 自动代码生成C#用户控件 - Automatic Code Generation C# User Control 编辑.tt文件以更改代码生成不会影响“运行自定义工具” - Editing .tt file to change code generation does nothing hiting “run custom tool” XAML绑定代码无法正常工作 - XAML binding code not working WPF / XAML:在文本框中查看/编辑XML文件 - WPF/XAML: Viewing/editing XML file in a TextBox 从列表中删除文件后,自动重命名filname无法正常工作 - Automatic rename filname not working fine after delete a file from list 由于XAML到.NET代码生成中的错误,此命名空间是否会发生冲突? - Is this namespace collision due to a bug in the XAML to .NET code generation? 自定义XAML后,滑块不起作用 - Slider not working after customization XAML
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM