关于数组排名大小的弹出窗口阻止我保存

时间:2014-10-31 22:06:11

标签: c# visual-studio-2010 multidimensional-array user-controls visual-c#-express-2010

我有一个具有多个属性的特定用户控件,包括一个属于另一个类的属性。该类包含public int[,] this[int x, int y](我不记得确切的术语)以及public int[,]属性和private int[,]字段。然后我有一个包含该控件的表单。每当我尝试编辑表单然后保存时,它都会让我拒绝保存,以下弹出窗口连续出现 3到6次

Exact error message (obtained with control+c on the window):

---------------------------
Microsoft Visual C# 2010 Express
---------------------------
Array rank '2' is too high.  Visual Studio can only save and load arrays with a rank of 1.
---------------------------
OK   
---------------------------

请注意,我不需要在设计器中保存到这些数组中的信息;每次ExampleControl内的值都设置为默认值。我甚至没有能力修改或看到这些阵列。


这里是重现所需的所有代码:

创建新的表单应用程序,然后删除生成的Form1.cs文件。

Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace ArrayRankIssue
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main() {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new ExampleForm());
        }
    }
}

ExampleClass.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ArrayRankIssue
{
    public class ExampleClass
    {
        private int[,] data = new int[2, 2];

        public int[,] Data {
            get {
                return data;
            }
            set {
                if (value.GetLength(0) != data.GetLength(0) || value.GetLength(1) != data.GetLength(1)) {
                    throw new ArgumentException("Argument must match size of array exactly.");
                }
                data = value;
            }
        }

        public int[,] this[int x, int y] {
            get {
                return data;
            }
            set {
                if (value.GetLength(0) != data.GetLength(0) || value.GetLength(1) != data.GetLength(1)) {
                    throw new ArgumentException("Argument must match size of array exactly.");
                }
                data = value;
            }
        }
    }
}

ExampleControl.cs

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

namespace ArrayRankIssue
{
    public partial class ExampleControl : UserControl
    {
        internal ExampleClass example = new ExampleClass();

        public ExampleClass Example {
            get {
                return example;
            }
            set {
                example = value;
            }
        }

        public ExampleControl() {
            InitializeComponent();
        }
    }
}

ExampleControl.Designer.cs

namespace ArrayRankIssue
{
    partial class ExampleControl
    {
        /// <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);
        }

        #region Component Designer generated code

        /// <summary> 
        /// Required method for Designer support - do not modify 
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent() {
            components = new System.ComponentModel.Container();
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        }

        #endregion
    }
}

ExampleForm.cs

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

namespace ArrayRankIssue
{
    public partial class ExampleForm : Form
    {
        public ExampleForm() {
            InitializeComponent();
        }
    }
}

ExampleForm.Designer.cs

namespace ArrayRankIssue
{
    partial class ExampleForm
    {
        /// <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);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent() {
            ArrayRankIssue.ExampleClass exampleClass8 = new ArrayRankIssue.ExampleClass();
            this.exampleControl1 = new ArrayRankIssue.ExampleControl();
            this.SuspendLayout();
            // 
            // exampleControl1
            // 
            this.exampleControl1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.exampleControl1.Example = exampleClass8;
            this.exampleControl1.Location = new System.Drawing.Point(12, 12);
            this.exampleControl1.Name = "exampleControl1";
            this.exampleControl1.Size = new System.Drawing.Size(150, 150);
            this.exampleControl1.TabIndex = 0;
            // 
            // ExampleForm
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(292, 273);
            this.Controls.Add(this.exampleControl1);
            this.Name = "ExampleForm";
            this.Text = "Form1";
            this.ResumeLayout(false);

        }

        #endregion

        private ExampleControl exampleControl1;
    }
}

创建所有这些文件后,选择 Build - &gt; Rebuild solution [note 1] 。然后,双击ExampleForm.cs,单击轮廓控件,然后按 left ,然后按键盘上的 [注2] 。然后,按 Control S ,您将多次收到相同的错误消息。


我正在寻找的方法是防止此消息弹出(并希望解决它弹出的原因,而不仅仅是压制它)。我找到了一个简单的解决方法,即关闭表单设计器窗口然后保存,但这似乎不切实际。成功保存后,表单显示正确,因此似乎也没有任何其他效果。

另一个提及此错误消息的内容是this question,但这没有提供有用的解决方案。


注1:此选项仅适用于&#34;专家模式&#34; - 如果您没有,请转到工具 - &gt; 设置 - &gt; 专家设置
注意2:这是必需的,以便Visual Studio修改该文件。如果没有完成,则无法强制进行第二次保存。

1 个答案:

答案 0 :(得分:1)

移动这些线条对我有用:

来自ExampleForm.Designer.cs

ArrayRankIssue.ExampleClass exampleClass2 = new ArrayRankIssue.ExampleClass();
this.exampleControl1.Example = exampleClass2;

ExampleForm.cs

  public partial class ExampleForm : Form
  {
    public ExampleForm()
    {
      InitializeComponent();
      ArrayRankIssue.ExampleClass exampleClass2 = new ArrayRankIssue.ExampleClass();
      this.exampleControl1.Example = exampleClass2;
    }
  }

解决方案2 如果您已经设置了属性,并且您不需要更改它们,请使用以下标记对其进行标记:

[ReadOnly(true)]
[Browsable(false)]

在您的示例中,将这些属性添加到Data中的thisExampleClass.cs

您必须更改代码,从表单中删除任何现有控件,然后编译然后重新添加

相关问题