在 C# Windows 窗体中,如果我不想修改设计器中的代码,我应该如何取消订阅后面代码中的事件?

问题描述 投票:0回答:1

C# Windows 窗体在其设计器文件中具有生成的 dispose 方法代码

partial class DetailRibbonForm2
{
    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.IContainer components = null;

    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
    protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
        {
            components.Dispose();
        }
        base.Dispose(disposing);
    }
   // etc

我想在代码隐藏文件中添加一些代码,以便在处理时取消订阅事件。

我该怎么做?

如果我尝试覆盖代码隐藏中的 Dispose,我会得到

错误CS0111:类型“DetailRibbonForm2”已经定义了一个名为的成员 使用相同的参数类型“Dispose”

c# winforms .net-8.0
1个回答
0
投票

您可能会注意到,

Dispose()
位于“Windows 窗体设计器生成的代码,请勿编辑”区域之外。所以,你可以安全地修改它。

另一点是,

.designer.cs
文件不是我期望手动编辑的东西,所以我个人更喜欢将生成的
Dispose()
实现从
form.designer.cs
移动到
form.cs
,然后扩展此实现,因为我需要。因此,较新的版本存在任何与表单设计器相关的问题。

© www.soinside.com 2019 - 2024. All rights reserved.