Winform 应用程序中的 MDIparent 返回对象引用未设置为对象的实例

发布于 2024-12-19 12:42:34 字数 1204 浏览 5 评论 0原文

我对这段代码有疑问。您可以在此处找到所有类。

如果我启动应用程序并且想要打开一个新表单,我会收到此错误:

NullReferenceException:未将对象引用设置为对象的实例。

主应用程序设置为 isMDIcontainer = true;

现在我收到这部分代码的错误:

private void PluginClick(object sender, EventArgs e)
{
    ToolStripMenuItem menu = (ToolStripMenuItem)sender;
    Plugin.PluginForm form = ((PluginInfo)menu.Tag).CreateInstance();
    form.MdiParent = this;   // Here is thrown the error
    form.Show();
}

Plugin.PluginForm is only an Extended Form。这是 CreateIstance() 方法:

public PluginForm CreateInstance()
{
    if (!File.Exists(FileName))
        return null;

    Assembly ass = Assembly.LoadFile(FileName);
    foreach (Type type in ass.GetTypes())
    {
        if (type.BaseType == typeof(PluginForm))
        {
           return (PluginForm)Activator.CreateInstance(type);
        }
    }
    return null;
}

在同一个 sebsite 中,有人说可以通过以下方式解决此错误:

必须在接口中声明属性system.window.formparentForm

但我不明白如何。

I have a problem with this code. You can find all classes here.

If I launch the application and I want open a new Form i receive this error:

NullReferenceException : Object reference not set to an instance of an object.

The main application is set to isMDIcontainer = true;

Now I received the error in this part of code:

private void PluginClick(object sender, EventArgs e)
{
    ToolStripMenuItem menu = (ToolStripMenuItem)sender;
    Plugin.PluginForm form = ((PluginInfo)menu.Tag).CreateInstance();
    form.MdiParent = this;   // Here is thrown the error
    form.Show();
}

Plugin.PluginForm is only an Extended Form. This is the CreateIstance() method:

public PluginForm CreateInstance()
{
    if (!File.Exists(FileName))
        return null;

    Assembly ass = Assembly.LoadFile(FileName);
    foreach (Type type in ass.GetTypes())
    {
        if (type.BaseType == typeof(PluginForm))
        {
           return (PluginForm)Activator.CreateInstance(type);
        }
    }
    return null;
}

In the same sebsite someone says that this error could may be resolved in this way:

You must declare property system.window.form parentForm in interface

but I didn't understand how.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

从﹋此江山别 2024-12-26 12:42:34

由于 FileName 错误(文件名或路径不正确),CreateInstance 很可能返回 null

它返回 null 的结果是 form 变量为 null 并且对其进行任何成员访问(如 form.MdiParent 将导致 NullReferenceException

请确保文件名正确并且该文件存在于搜索路径中。

Chances are good that CreateInstance is returning a null because the FileName is wrong (incorrect filename or path).

The result of it returning a null is that the form variable is null and any member access on it (as in form.MdiParent will result in a NullReferenceException.

Make sure that the filename is correct and that the file exists in the path it is searched on.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文