防止 Visual Studio 加载自定义分部类作为设计器 [英] Prevent Visual Studio load a custom partial class as a designer
问题描述
在 Visual Studio 中,有很多设计器编辑器、Windows 窗体、XAML、安装程序等.
In Visual Studio, there are lot of designer editors, Windows Forms, XAML, Installer, and others.
有时,我会创建一个新的源代码作为部分类(自定义)来分离逻辑.
Sometimes, I create a new source code as partial class (custom) to separate the logic.
例如:
- 仅部分代码:类 Form1 : Windows.Forms.Form → Form1.cs
- 仅部分设计器:部分类 Form1:Form → Form1.Designer.cs
- 部分类(自定义):部分类 form1:Form → Form.Print.cs
在这最后,我只包括打印方法和属性,然后,当我在解决方案资源管理器中双击这个文件时,总是打开设计器,而不是代码编辑器,当然,我尝试使用 F7 或右键单击来执行此操作,但是当我与同事共享该项目时,它成为一个问题.
In this last, I include print only methods and properties, and then, when I double click this file on Solution Explorer, always open the designer, instead of code editor, of course, I try use F7 or right-click to do that, but when I share the project with co-workers, it becomes a problem.
有人知道如何避免这种行为吗?也许是一个类属性!?
Anyone know how to avoid this behavior? Maybe a class attribute!?
推荐答案
作为一个选项,在文件的开头添加一个由 [DesignerCategory("")]
修饰的虚拟类.这将 DesignerCategory
的行为限制为设计器尝试加载的第一个类.
As an option add a dummy class decorated by [DesignerCategory("")]
at the beginning of your file. This limits the behavior of DesignerCategory
to the first class which the designer tries to load.
不优雅但有效:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WindowsFormsApplication1
{
[System.ComponentModel.DesignerCategory("")]
public class Dummy { }
public partial class Form1
{
}
}
这篇关于防止 Visual Studio 加载自定义分部类作为设计器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!