【问题标题】:Test.MainForm.Dispose(bool): no ​suitable method to override (CS0115)Test.MainForm.Dispose(bool):没有合适的方法来覆盖 (CS0115)
【发布时间】:2020-01-01 09:19:35
【问题描述】:

我正在编写一个 C# 表单应用程序。以下代码是我的“MainForm.cs”。

using CefSharp;
using CefSharp.WinForms;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WinFormsChromium
{
public partial class MainForm : Form
{
    public ChromiumWebBrowser browser;

    public void InitBrowser()
    {
        Cef.Initialize(new CefSettings());
        browser = new ChromiumWebBrowser("www.google.com");
        this.Controls.Add(browser);
        browser.Dock = DockStyle.Fill;

        browser.LoadingStateChanged += browser_LoadingStateChanged; 
    }

    private void browser_LoadingStateChanged(object sender, LoadingStateChangedEventArgs e)
    {
        if (e.IsLoading == false)
        {
            browser.ExecuteScriptAsync("alert('All Resources Have Loaded');");
        }
    }

    public MainForm()
    {
        InitializeComponent();
        InitBrowser();
    }
}
}

该文件没有问题。我的“MainForm.Designer.cs”有问题。下面的代码就是那个文件。

namespace Test
{
partial class MainForm
{
    private System.ComponentModel.IContainer components = null;
    protected override void Dispose(bool disposing)
    {
        if (disposing) {
            if (components != null) {
                components.Dispose();
            }
        }
        base.Dispose(disposing);
    }
    private void InitializeComponent()
    {
        this.SuspendLayout();
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(534, 279);
        this.Name = "MainForm";
        this.Text = "Test";
        this.ResumeLayout(false);
    }
}
}

错误是“Test.MainForm.Dispose(bool): no ​​​suitable method to override (CS0115)”。我认为问题在于“受保护的覆盖无效处置(布尔处置)”。我该如何解决?

【问题讨论】:

  • 你是否让组件继承了IDisposable?
  • @yaakov : 组件类需要是 IDisposable。
  • 我该怎么做?
  • IContainer 是 also disposable,无论如何,如果不是,它会产生与 OP 不同的编译器错误。
  • 当 foo.designer.cs 文件中的 Form 声明与 foo.cs 文件不同步时发生。在这种情况下,它是命名空间名称。您可以通过右键单击标识符名称并选择重命名来避免它。 IDE 足够聪明,可以随处更改。

标签: c#


【解决方案1】:

问题出在不同的命名空间中

.cs 文件有命名空间WinFormsChromium,Designer.cs 文件有Test

请在 .Designer.cs 文件中将 namespace Test 替换为 namespace WinFormsChromium 以使 partial class MainForm 部件彼此可见

【讨论】:

  • .Designer 是自动生成的。编辑它没有意义。
  • @GSerg 你是对的,但似乎 MainForm.cs 命名空间已从 Test 更改为 WinFormsChromium (只需查看源代码)。命名空间必须相同才能使部分类部分彼此可见
  • 是的,它必须是相同的,但必须在另一个文件中进行更改,或者可能在项目中的“默认命名空间”设置中进行。
  • @GSerg Default namespace 选项不会更改现有的命名空间。当相关.cs 文件中的命名空间发生更改时,我在.Designer.cs 文件中进行了很多更改
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-11-05
  • 2019-07-07
  • 1970-01-01
  • 2018-09-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多