0

您好我正在尝试将使用 2D 数组动态创建的这个 4x4 按钮网格的颜色状态保存到 xml 文档中:

在此处输入图像描述

但是,当我按保存时,我不断收到此消息:

如果我对按钮使用一维数组,我可以完成这项工作,但这不会给我想要的网格,但是当我对按钮使用二维数组时,它不会工作:

在此处输入图像描述

我可以改变什么,以便我可以让它工作任何建议都非常感谢:

这是我的代码:

FormState班级:

public class FormState
{
    public string ButtonBackColor { get; set; }
}

表格代码:

public partial class Form1 : Form
{
        int col = 4;
        int row = 4;
        Button[,] buttons;
        FormState[,] states;
        public Form1()
        {
            InitializeComponent();
            buttons = new Button[col, row];
            states = new FormState[col, row];
        }

        public void placeRows()
        {
            for (int r = 0; r < row; r++)
            {
                createColumns(r);
            }
        }

        public void createColumns(int r)
        {
            int s = r * 25; //gap
            for (int c = 0; c < col; c++)
            {
                buttons[r, c] = new Button();
                buttons[r, c].SetBounds(75 * c, s, 75, 25);
                buttons[r, c].Text = Convert.ToString(c);
                buttons[r, c].Click += new EventHandler(grid_Click);
                panel1.Controls.Add(buttons[r, c]);
            }
        }

        int count = 0;
        //backcolor change
        void grid_Click(object sender, EventArgs e)
        {
            Button button = sender as Button;

            if (count == 0)
            {
                button.BackColor = Color.Red;
                count++;
            }

            else if (count == 1)
            {
                button.BackColor = Color.Blue;
                count--;
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            placeRows();

            if (File.Exists("config.xml"))
            {
                loadConfig();
            }

            for (int i = 0; i < col; ++i)
            {
                for (int j = 0; j < row; ++j)
                {
                    if (states[i,j] != null)
                    {
                        buttons[i,j].BackColor = ColorTranslator.FromHtml(states[i,j].ButtonBackColor);
                    }
                }
            }
        }

        //method to load file
        private void loadConfig()
        {
            XmlSerializer ser = new XmlSerializer(typeof(FormState[]));
            using (FileStream fs = File.OpenRead("config.xml"))
            {
                states = (FormState[,])ser.Deserialize(fs);
            }
        }

        private void writeConfig()
        {
            for (int i = 0; i < col; i++)
            {
                for (int j = 0; j < row; j++)
                {
                    if (states[i,j] == null)
                    {
                        states[i,j] = new FormState();
                    }
                    states[i,j].ButtonBackColor = ColorTranslator.ToHtml(buttons[i,j].BackColor);
                }

                using (StreamWriter sw = new StreamWriter("config.xml"))
                {
                    XmlSerializer ser = new XmlSerializer(typeof(FormState[]));
                    ser.Serialize(sw, states);
                }
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            writeConfig();
        }
    }
4

2 回答 2

1

这可能不是一个理想的解决方案(我还没有尝试过,所以它甚至可能不起作用),但您可以创建一个嵌套数组而不是二维数组。就像是

FormStates[][] states = new FormStates[row][];
for(Int32 i = 0; i < row; i++)
{
    states[i] = new FormStates[col];
}

而不是使用索引states[i, j],您将使用states[i][j]. 由于一维数组是可序列化的,这可能有效。

编辑

稍长的示例,基于您的代码:

public partial class Form1 : Form
{
    int col = 4;
    int row = 4;
    Button[][] buttons;
    FormState[][] states;
    public Form1()
    {
        InitializeComponent();
        buttons = new Button[col][];
        states = new FormState[col][];
        for(Int32 c = 0; c < col; c++)
        {
            buttons[c] = new Button[row];
            states[c] = new FormState[row];
        }
    }

    public void createColumns(int r)
    {
        int s = r * 25; //gap
        for (int c = 0; c < col; c++)
        {
            buttons[r][c] = new Button();
            buttons[r][c].SetBounds(75 * c, s, 75, 25);
            buttons[r][c].Text = Convert.ToString(c);
            buttons[r][c].Click += new EventHandler(grid_Click);
            panel1.Controls.Add(buttons[r][c]);
        }
    }
}

这应该足以为您提供更改其余代码的语法。

您还需要将您的XmlSerializer声明更改为使用typeof(FormState[][]),而不仅仅是typeof(FormState[]).

于 2013-03-04T03:12:43.273 回答
0

诚然,我对此并没有太多考虑,但根据评论,您无法序列化多维数组,因此您可以:

 [Serializable]
    public class FormState
    {
        public int RowIndex { get; set; }
        public int ColIndex { get; set; }
        public string BackColor { get; set; }
    }

    [Serializable]
    public class Layout : Collection<FormState> { 

        public Layout(){}
    }

..

        public void SomeCallingMethod() {
            Layout l = new Layout();
            foreach (FormState fs in l) {
                buttons[fs.RowIndex, fs.ColIndex].BackColor = ColorTranslator.FromHtml(fs.BackColor);
            }     
        }

或在必要时使用 List 并将其序列化。

于 2013-03-04T02:04:44.253 回答