C# 带设计器的部分嵌套表单类
我正在制作一个类似MessageBox的类(MessageBoxCustom)。 我希望在一个单独的文件中有一个支持设计器的表单,这样我就可以通过VisualStudio(MessageBoxCustomDialog)修改外观 我还想让我的MessageBox之外的代码无法访问MessageBoxCustomDialog,我正在嵌套MessageBoxCustomDialog。我想将其移动到一个单独的文件中,以便获得设计器支持。也许使用部分类?等级制度将如何发展C# 带设计器的部分嵌套表单类,c#,winforms,dialog,partial,nested-class,C#,Winforms,Dialog,Partial,Nested Class,我正在制作一个类似MessageBox的类(MessageBoxCustom)。 我希望在一个单独的文件中有一个支持设计器的表单,这样我就可以通过VisualStudio(MessageBoxCustomDialog)修改外观 我还想让我的MessageBox之外的代码无法访问MessageBoxCustomDialog,我正在嵌套MessageBoxCustomDialog。我想将其移动到一个单独的文件中,以便获得设计器支持。也许使用部分类?等级制度将如何发展 using System; us
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace System.Windows.Forms
{
public static class MessageBoxCustom
{
public static void Show()
{
(new MessageBoxCustomDialog()).ShowDialog();
}
private class MessageBoxCustomDialog : Form
{
}
}
}
使MessageBoxCustomDialog成为私有的部分内部类
private partial class MessageBoxCustomDialog : Form
{}
您必须使
MessageBoxCustom
部分具有与MessageBoxCustomDialog
文件1
using System.Windows.Forms;
namespace System.Windows.Forms
{
public static partial class MessageBoxCustom
{
public static void Show()
{
(new MessageBoxCustomDialog()).ShowDialog();
}
private partial class MessageBoxCustomDialog : Form
{
}
}
}
文件2
using System.Windows.Forms;
namespace System.Windows.Forms
{
public static partial class MessageBoxCustom
{
private partial class MessageBoxCustomDialog : Form
{
// designer code
}
}
}
您可能会看到此链接[限制部分]Visual Studio设计器无法帮助您设计嵌套类。它不是为这个而设计的。它检查文件中第一个最外层类的类型,然后决定使用哪个设计器 如果只是为了设计表单的布局,我建议像往常一样设计它。完成项目后,可以用外部类(在两个文件中)包围该类,并将其设置为私有 完成工作后,只需将dialog类复制并粘贴到外部类中,并将其设置为私有。如果你不得不重做设计,它只是再次复制和粘贴 MessageBoxCustomDialog.cs:
namespace System.Windows.Forms
{
// make sure this is the first class in the file (required by designer)
public partial class MessageBoxCustomDialog : Form
{
public MessageBoxCustomDialog()
{
InitializeComponent();
}
}
public static partial class MessageBoxCustom
{
public static void Show()
{
new MessageBoxCustomDialog().ShowDialog();
}
// put the MessageBoxCustomDialog class here when you are done
}
}
MessageBoxCustomDialog.Designer.cs:
namespace System.Windows.Forms
{
partial class MessageBoxCustomDialog
{
...
}
partial class MessageBoxCustom
{
// put the MessageBoxCustomDialog class here when you are done
}
}
命名空间中定义的元素不能显式声明为private、protected或protected internal。仍然不支持file2。