加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 07:48:18 所属栏目:百科 来源:网络整理
导读:我在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 我可以成功构建并运行该应用程序.
我在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)BinMicrosoft.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>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读