微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c# – 使用新的CSPROJ格式时,在代码隐藏中无法识别WPF控件

我在Visual Studio 2017中使用新的CSPROJ格式创建了一个 WPF应用程序.

通过

> setting the LanguageTargets,
> setting the generator for XAML files,
> setting the build action for App.Xaml,
>和forcing the debug executable

我可以成功构建并运行该应用程序.但是,我有一个问题,即代码编辑器无法识别XAML中的任何控件,因此我在编辑器中得到错误错误而没有智能感知.

重现步骤

>发布VS 2017
>创建一个新的“WPF应用程序(.NET Framework)”C#项目
>编辑csproj文件,如下所示:

项目文件

<Project Sdk="Microsoft.NET.Sdk" Toolsversion="15.0">
  <PropertyGroup>
    <LanguageTargets>$(MSBuildExtensionsPath)\$(VisualStudioVersion)\Bin\Microsoft.CSharp.targets</LanguageTargets>
    <TargetFramework>net45</TargetFramework>
    <ProjectGuid>{030D04DA-D603-4D4C-95F7-B6F725A6829E}</ProjectGuid>
  </PropertyGroup>
  <PropertyGroup>
    <OutputType>WinExe</OutputType>
  </PropertyGroup>
  <PropertyGroup>
    <StartupObject />
  </PropertyGroup>
  <ItemGroup>
    <ApplicationDeFinition Include="App.xaml">
      <Generator>MSBuild:Compile</Generator>
      <SubType>Designer</SubType>
    </ApplicationDeFinition>
    <Page Include="MainWindow.xaml">
      <SubType>Designer</SubType>
      <Generator>MSBuild:Compile</Generator>
    </Page>
    <Compile Update="**\*.xaml.cs" SubType="Designer" DependentUpon="%(Filename)" />
  </ItemGroup>
  <ItemGroup>
    <Reference Include="PresentationCore" />
    <Reference Include="PresentationFramework" />
    <Reference Include="System.Xaml" />
    <Reference Include="WindowsBase" />
  </ItemGroup>
</Project>

>将一个名为“button1”的按钮添加到MainWindow.xaml,如下所示:

MainWindow.xaml

<Window x:Class="WpfApp1.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:WpfApp1"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Button Name="button1" Width="100" Height="50">Click me!</Button>
    </Grid>
</Window>

>尝试在“MainWindow.xaml.cs”中的代码中设置按钮标题.

您将看到intellisense无法识别按钮已定义:

但是,项目构建并成功运行(请注意,您需要手动运行可执行文件 – F5不起作用…)

解决方法

您之前的答案略有改进,包括.g.cs文件,但将它们标记为不可见,因此它们不会显示解决方案中.然后,您还需要将BaseIntermediateOutputPath标记为不可见,否则它将显示为空文件夹.

这给出了相同的行为,但看起来更整洁,因为您没有在解决方案资源管理器中看到obj文件夹.

<ItemGroup>
  <ApplicationDeFinition Include="App.xaml">
    <Generator>MSBuild:Compile</Generator>
    <SubType>Designer</SubType>
  </ApplicationDeFinition>
  <Page Include="**\*.xaml" SubType="Designer" Generator="MSBuild:Compile" Exclude="App.xaml" />
  <Compile Update="**\*.xaml.cs" SubType="Designer" DependentUpon="%(Filename)" />
  <Compile Include="$(IntermediateOutputPath)*.g.cs" Visible="false" />
  <None Include="$(BaseIntermediateOutputPath)" Visible="false" />
</ItemGroup>

原文地址:https://www.jb51.cc/csharp/99600.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐