Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 带设计器的部分嵌套表单类_C#_Winforms_Dialog_Partial_Nested Class - Fatal编程技术网

C# 带设计器的部分嵌套表单类

C# 带设计器的部分嵌套表单类,c#,winforms,dialog,partial,nested-class,C#,Winforms,Dialog,Partial,Nested Class,我正在制作一个类似MessageBox的类(MessageBoxCustom)。 我希望在一个单独的文件中有一个支持设计器的表单,这样我就可以通过VisualStudio(MessageBoxCustomDialog)修改外观 我还想让我的MessageBox之外的代码无法访问MessageBoxCustomDialog,我正在嵌套MessageBoxCustomDialog。我想将其移动到一个单独的文件中,以便获得设计器支持。也许使用部分类?等级制度将如何发展 using System; us

我正在制作一个类似MessageBox的类(MessageBoxCustom)。 我希望在一个单独的文件中有一个支持设计器的表单,这样我就可以通过VisualStudio(MessageBoxCustomDialog)修改外观

我还想让我的MessageBox之外的代码无法访问MessageBoxCustomDialog,我正在嵌套MessageBoxCustomDialog。我想将其移动到一个单独的文件中,以便获得设计器支持。也许使用部分类?等级制度将如何发展

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。