winforms vs2019运行SDK项目(框架4.7.2)中的控件和窗体缺少图标和视图设计器选项

tf7tbtn2  于 2023-03-31  发布在  其他
关注(0)|答案(3)|浏览(163)

到目前为止的总结回答:如果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格式的,尽管它是framework 4.7.2,我想这可能与它有关。
我用的是Framework 4.7.2 with an sdk project
Project Sdk=“Microsoft.NET.Sdk.WindowsDesktop”
我在运行VS2019 16.1.4的另一台计算机上 checkout 了代码,它构建正常。
我用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文件在我的答案中。

pftdvrlh

pftdvrlh1#

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

<Compile Include="form1.cs">
      <SubType>Form</SubType>
    </Compile>
5anewei6

5anewei62#

原来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签入源代码控制的困境。我以前认为这是没有必要的,但现在看来是必要的。加上名称“user”意味着它是用户特定的。

fzsnzjdm

fzsnzjdm3#

概述

此解决方案使用globs(模式匹配)进行维护。

*不需要包含每个表单文件

问题

  • SDK projects目前在windows窗体中已失效(截至2023-03-24
  • 唯一的方法(我发现的)是手动编辑项目文件(在我的例子中是.vbproj

溶液

  • 为所有表单创建一个“表单”文件夹
  • 重命名表单文件以“Form”结尾
  • 使用glob匹配查找这些文件(并应用<SubType>Form</SubType>
  • 参见下面的项目文件

项目文件

<Project Sdk="Microsoft.NET.Sdk">
    ...
    
    <ItemGroup Label="Make Winforms Designer work">
        <!-- Mark startup page as a form -->
        <Compile Update="MainForm.vb">
            <SubType>Form</SubType>
        </Compile>

        <!-- 
            Mark all other forms in "Forms" folder as a form
            - File name must also end with "Form.vb"
        -->
        <Compile Update="Forms\**\*Form.vb">
            <SubType>Form</SubType>
        </Compile>
    </ItemGroup>
    
    ...
</Project>

相关问题