使用新的 CSPROJ 格式时,代码隐藏中无法识别 WPF 控件 [英] WPF controls not recognized in code-behind when using new CSPROJ format

查看:61
本文介绍了使用新的 CSPROJ 格式时,代码隐藏中无法识别 WPF 控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

来自

  • 重现步骤

    1. 发布 VS 2017
    2. 创建一个新的WPF 应用程序 (.NET Framework)"C# 项目
    3. 编辑 csproj 文件如下:

    项目文件:

    <属性组><LanguageTargets>$(MSBuildExtensionsPath)\$(VisualStudioVersion)\Bin\Microsoft.CSharp.targets</LanguageTargets><TargetFramework>net45</TargetFramework><ProjectGuid>{030D04DA-D603-4D4C-95F7-B6F725A6829E}</ProjectGuid></PropertyGroup><属性组><OutputType>WinExe</OutputType></PropertyGroup><属性组><启动对象/></PropertyGroup><项目组><ApplicationDefinition Include="App.xaml"><Generator>MSBuild:Compile</Generator><SubType>设计器</SubType></应用定义><Page Include="MainWindow.xaml"><SubType>设计器</SubType><Generator>MSBuild:Compile</Generator></页面><Compile Update="**\*.xaml.cs" SubType="Designer" DependentUpon="%(Filename)"/></项目组><项目组><Reference Include="PresentationCore"/><Reference Include="PresentationFramework"/><Reference Include="System.Xaml"/><Reference Include="WindowsBase"/></项目组></项目>

    1. 向 MainWindow.xaml 添加一个名为button1"的按钮,如下所示:

    MainWindow.xaml

    <网格><Button Name="button1" Width="100" Height="50">点击我!</Button></网格></窗口>

    1. 尝试在MainWindow.xaml.cs"中的代码中设置按钮标题.

    你会看到,intellisense 无法识别按钮已定义:

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

    解决方案

    对您之前的答案的一个小改进是包含 .g.cs 文件,但将它们标记为不可见,这样它们就不会显示在解决方案中.然后,您还需要将 BaseIntermediateOutputPath 标记为不可见,否则它会显示为一个空文件夹.

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

    <ApplicationDefinition Include="App.xaml"><Generator>MSBuild:Compile</Generator><SubType>设计器</SubType></应用定义><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"/></项目组>

    I've created a WPF application using the new CSPROJ format in Visual Studio 2017.

    By

    I can successfully build and run the application. However, I have an issue in that the code editor does not recognize any controls placed in the XAML, so I get erroneous errors and no intellisense in the editor.

    Steps to reproduce

    1. Launch VS 2017
    2. Create a new "WPF App (.NET Framework)" C# project
    3. Edit the csproj file to look like:

    Project file:

    <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>
    

    1. Add a button called "button1" to MainWindow.xaml as below:

    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>
    

    1. Try and set the button caption in code in "MainWindow.xaml.cs".

    You will see that intellisense does not recognise that the button is defined:

    However, the project builds and runs successfully (note you will need to run the executable manually - F5 doesn't work...)

    解决方案

    A slight improvement on your previous answer is to include the .g.cs files, but mark them as not visible so they do not show in the solution. You will then also need to mark the BaseIntermediateOutputPath as not visible otherwise it shows up as an empty folder.

    This gives the same behaviour but looks tidier as you're not seeing the obj folder in the solution explorer.

    <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>
    

    这篇关于使用新的 CSPROJ 格式时,代码隐藏中无法识别 WPF 控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆