8

到目前为止的总结答案:如果 csproj.user 文件存在,表单和控件仅在设计器中打开。

问题故事:

我有 2 个控件都继承自基本控件。但是,解决方案资源管理器中缺少控制图标 缺少图标

好的控制源从

public partial class UniTabMaterialsControl : UnitabBaseControl
{
    public UniTabMaterialsControl()
    {
        InitializeComponent();
    }

坏的控制源开始于

public partial class UniTabMaterialsControl : UnitabBaseControl
{
     
    public UniTabMaterialsControl()
    {
        InitializeComponent();
    }

基本控制类是

public class UnitabBaseControl : UserControl
{
}

我尝试关闭并重新打开 Visual Studio

[更新]

我添加了一个新的用户控件并将其设置为也继承自 UnitabBaseControl ,奇怪的是行为更正了。

然后我能够删除新控件并保持正确的行为

Git 显示 UnitabBaseControl.cs 发生了变化,但除了 UserControl 文本的颜色之外,我没有看到其他变化

基类颜色改变

然后我对整个解决方案做了一个全新的克隆。这次从 UnitabBaseControl 继承的所有控件上都显示了不正确的行为

[更新]

现在我在表单上遇到同样的问题

表格没有显示正确的图标

using System.Windows.Forms;

namespace SBD.VivSnap.UI
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
    }
}

using System.Windows.Forms;

namespace SBD.VivSnap.UI
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }
    }
}

[更新]

我忘了提到我的项目是启动项目所引用的库项目。

我在启动项目中删除然后重新添加了库项目引用。然后所有表单和控件都正确显示。

我在 Git 中找不到任何更改来说明为什么该解决方案现在有效。

从那以后我注意到我的主要项目中的问题

主项目中的问题

[更新]

事实证明,我的项目是 .sdk 格式,即使它是框架 4.7.2 我认为这可能与它有关。

我正在使用带有 sdk 项目的 Framework 4.7.2

Project Sdk="Microsoft.NET.Sdk.WindowsDesktop"

我将代码检查到另一台运行 VS2019 16.1.4 的计算机上,它构建正常。

我正在使用 16.11.4

[更新] 现在我已经更新到 16.11.5 并再次克隆,但仍然有问题。

[更新]项目文件是

<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
  <PropertyGroup>
    <TargetFramework>net472</TargetFramework>
    <OutputType>WinExe</OutputType>
    <AssemblyName>Main</AssemblyName>
    <SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\</SolutionDir>
    <RestorePackages>true</RestorePackages>
    <GenerateAssemblyInfo>false</GenerateAssemblyInfo>
    <UseWindowsForms>true</UseWindowsForms>
  </PropertyGroup>
  <PropertyGroup>
    <StartupObject>SBD.VivSnap.Main.Program</StartupObject>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
    <PackageReference Include="Microsoft.Data.SqlClient">
      <Version>2.0.1</Version>
    </PackageReference>
    <PackageReference Include="Microsoft.EntityFrameworkCore">
      <Version>3.1.12</Version>
    </PackageReference>
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer">
      <Version>3.1.12</Version>
    </PackageReference>
    <PackageReference Include="NuGet.Build.Tasks.Pack" Version="5.7.0">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
    <PackageReference Include="System.ComponentModel.Annotations" Version="5.0.0" />
    <PackageReference Include="System.Data.DataSetExtensions" Version="4.5.0" />
  </ItemGroup>
  <ItemGroup>
    <ProjectReference Include="..\SnapInUI\SBD.VivSnap.UI.csproj" />
  </ItemGroup>
  <ItemGroup>
    <Compile Update="Properties\Settings.Designer.cs">
      <DesignTimeSharedInput>True</DesignTimeSharedInput>
      <AutoGen>True</AutoGen>
      <DependentUpon>Settings.settings</DependentUpon>
    </Compile>
  </ItemGroup>
  <ItemGroup>
    <None Update="Properties\Settings.settings">
      <Generator>SettingsSingleFileGenerator</Generator>
      <LastGenOutput>Settings.Designer.cs</LastGenOutput>
    </None>
  </ItemGroup>
</Project>

csproj.user 文件在我的回答中。

4

2 回答 2

3

表单的编译类型可能未在 .csproj 文件中正确设置为“表单”。打开 .csproj 文件并确保在提到“form1.cs”的任何地方都有这行代码:

<Compile Include="form1.cs">
      <SubType>Form</SubType>
    </Compile>
于 2021-10-13T14:09:08.993 回答
1

事实证明 csproj.user 文件不在源代码管理中。当我将 myproject.csproj.user 复制到项目文件夹并在 VS 中打开时,表单正确显示。

文件内容是

<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup />
  <ItemGroup>
    <Compile Update="Form1.cs">
      <SubType>Form</SubType>
    </Compile>
    <Compile Update="Form2.cs">
      <SubType>Form</SubType>
    </Compile>
  </ItemGroup>
</Project>

这让我陷入了是否将 csproj.user 签入源代码控制的困境。我以前认为这不是必需的,但似乎确实如此。加上名称“用户”意味着它是特定于用户的。

于 2021-10-19T04:31:49.370 回答