未使用System.ComponentModel.IContainer组件?

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

我正在尝试清除 C# 项目中的一些警告,其中有几个警告是:

Warning 1 The field 'Namespace.Class.components' is assigned but its value is 
never used  E:\WorkspacePath\Sources\Project\WorkItems\Class.designer.cs

如果我查看代码,我可以发现:

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

我看过,这个确实是设计师用过的,但他好像根本没用过。如果不是评论,我会立即删除这一行。 评论该行似乎并没有改变设计器的行为,我仍然可以毫无问题地打开我的菜单视图。

所以我的问题是,当我在编译时收到警告时,删除这一行是否安全,或者这个警告相当“正常”?我应该用它做什么?我是否忘记对这个组件执行某些操作?

谢谢您的帮助,

c# designer
3个回答
4
投票

在设计器文件中手动编辑代码通常不是一个好主意,因为设计器可能会感到困惑并导致一些意外的行为。目前可能还没有使用它,但某些控件(如 imagelist)将此 IContainer 作为依赖项。

您收到编译器警告的原因可能是因为您缺少标准生成的 Dispose 方法重写,这会阻止警告。这是:

    /// <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);
    }

0
投票

相关问题:

我收到了一堆这样的编译器警告:

警告 CS0649 字段“CreateMethodForm.components”从未被分配,并且始终具有默认值 null

我有很好的处置代码。

但我有:

private System.ComponentModel.IContainer components;

我需要更改为:

private System.ComponentModel.IContainer components = null;

0
投票

命名空间 UnityModManagerNet.Installer { 部分类 DownloadExtraFiles { /// /// Обязательная переменная конструктора. /// 私有 System.ComponentModel.IContainer 组件 = null;

    /// <summary>
    /// Освободить все используемые ресурсы.
    /// </summary>
    /// <param name="disposing">истинно, если управляемый ресурс должен быть удален; иначе ложно.</param>
    protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
        {
            components.Dispose();
        }
        base.Dispose(disposing);
    }

    #region Код, автоматически созданный конструктором форм Windows

    /// <summary>
    /// Требуемый метод для поддержки конструктора — не изменяйте 
    /// содержимое этого метода с помощью редактора кода.
    /// </summary>
    private void InitializeComponent()
    {
        this.status = new System.Windows.Forms.Label();
        this.progressBar1 = new System.Windows.Forms.ProgressBar();
        this.SuspendLayout();
        // 
        // status
        // 
        this.status.Location = new System.Drawing.Point(12, 9);
        this.status.Name = "status";
        this.status.Size = new System.Drawing.Size(306, 43);
        this.status.TabIndex = 2;
        this.status.Text = "Downloading...";
        this.status.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
        // 
        // progressBar1
        // 
        this.progressBar1.Location = new System.Drawing.Point(12, 55);
        this.progressBar1.Name = "progressBar1";
        this.progressBar1.Size = new System.Drawing.Size(306, 23);
        this.progressBar1.TabIndex = 3;
        // 
        // DownloadExtraFiles
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(330, 94);
        this.Controls.Add(this.progressBar1);
        this.Controls.Add(this.status);
        this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
        this.MaximizeBox = false;
        this.MinimizeBox = false;
        this.Name = "DownloadExtraFiles";
        this.Text = "Extra Files";
        this.ResumeLayout(false);

    }

    #endregion

    public System.Windows.Forms.Label status;
    private System.Windows.Forms.ProgressBar progressBar1;
}

}

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