如何将图像序列化为自定义DesignerSerializer

时间:2015-05-26 10:33:49

标签: c# serialization codedom

我为拥有数据网格视图的用户控件制作了自定义设计器。

现在我正在尝试序列化datagridview的columns属性。

该集合可以包含DataGridViewTextBoxColumn,DataGridViewComboBoxColumn,DataGridViewButtonColumn等。

对于DataGridViewImageColumn,我从资源文件中获取图像。 我无法弄清楚如何从我的自定义CodeDomSerializer序列化图像属性。 这是代码:

public class PanelDataGridViewCodeDomSerializer : CodeDomSerializer
{
    public override object Deserialize(IDesignerSerializationManager manager, object codeObject)
    {
        CodeDomSerializer baseClassSerializer = (CodeDomSerializer)manager.GetSerializer(typeof(PanelDataGridView).BaseType, typeof(CodeDomSerializer));
        return baseClassSerializer.Deserialize(manager, codeObject);
    }
    public override object Serialize(IDesignerSerializationManager manager, object value)
    {
        CodeDomSerializer baseClassSerializer = (CodeDomSerializer)manager.GetSerializer(typeof(PanelDataGridView).BaseType, typeof(CodeDomSerializer));
        // serialize the UserControl
        object codeObject = baseClassSerializer.Serialize(manager, value);
        if (codeObject is CodeStatementCollection)
        {
            CodeStatementCollection statements = (CodeStatementCollection)codeObject;
            // The code statement collection is valid, so add a comment.
            string commentText = "This comment was added to this object by a custom serializer.";
            CodeCommentStatement comment = new CodeCommentStatement(commentText);
            statements.Insert(0, comment);
            // serialize the inner DataGridView's columns
            if (value is PanelDataGridView)
            {
                DataGridViewColumnCollection innercolumns = (value as PanelDataGridView).zColumns;
                // declare the variable collection of columns in the inner DataGridView 
                List<CodeVariableReferenceExpression> parameter_list = new List<CodeVariableReferenceExpression>();
                CodeArrayCreateExpression createArray = null;
                CodeMethodInvokeExpression methodcall = null;

                int i = 1;

                CodeStatementCollection col_Statements = null;
                // Type is DataGridViewTextBoxColumn
                foreach (DataGridViewColumn col in innercolumns)
                {
                    /// serialize each column
                    col_Statements = new CodeStatementCollection();

                    CodeObjectCreateExpression col_ObjectCreate = new CodeObjectCreateExpression(col.GetType());

                    CodeVariableDeclarationStatement col_VariableDeclaration = new CodeVariableDeclarationStatement(col.GetType(), "column" + i.ToString());

                    CodeAssignStatement col_Assign_Create = new CodeAssignStatement(new CodeVariableReferenceExpression("column" + i.ToString()), col_ObjectCreate);
                    // serialize the DataPropertyName property of the column
                    CodeAssignStatement col_Assign_data_property_name = new CodeAssignStatement(new CodeVariableReferenceExpression("column" + i.ToString() + ".DataPropertyName"), new CodePrimitiveExpression(col.DataPropertyName));
                    // serialize the HeaderText property of the column
                    CodeAssignStatement col_Assign_header_text = new CodeAssignStatement(new CodeVariableReferenceExpression("column" + i.ToString() + ".HeaderText"), new CodePrimitiveExpression(col.HeaderText));

                    CodeAssignStatement col_Assign_image = null;
                    CodeAssignStatement col_Assign_image_layout = null;
                    if (col.GetType() == typeof(DataGridViewImageColumn))
                    {
                        DataGridViewImageColumn colImage = (DataGridViewImageColumn)col;
                        // serialize the Image property of the column
                        col_Assign_image = new CodeAssignStatement(new CodeVariableReferenceExpression("column" + i.ToString() + ".Image"), new CodePrimitiveExpression(colImage.Image.ToString()));
                        // serialize the ImageLayout property of the column
                        col_Assign_image_layout = new CodeAssignStatement(new CodeVariableReferenceExpression("column" + i.ToString() + ".ImageLayout"), new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(typeof(DataGridViewImageCellLayout)), colImage.ImageLayout.ToString()));    
                    }
                    // serialize the Resizable property of the column
                    //CodeAssignStatement col_Assign_resizable = new CodeAssignStatement(new CodeVariableReferenceExpression("column" + i.ToString() + ".Resizable"), new CodeTypeReference(col.Resizable));
                    CodeAssignStatement col_Assign_resizable = new CodeAssignStatement(new CodeVariableReferenceExpression("column" + i.ToString() + ".Resizable"), new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(typeof(DataGridViewTriState)),col.Resizable.ToString()));
                    // serialize the Visible property of the column
                    CodeAssignStatement col_Assign_visible = new CodeAssignStatement(new CodeVariableReferenceExpression("column" + i.ToString() + ".Visible"), new CodePrimitiveExpression(col.Visible));
                    // serialize the Width property of the column
                    CodeAssignStatement col_Assign_width = new CodeAssignStatement(new CodeVariableReferenceExpression("column" + i.ToString() + ".Width"), new CodePrimitiveExpression(col.Width));

                    CodeFieldReferenceExpression col_FieldReference = base.SerializeToExpression(manager, col) as CodeFieldReferenceExpression;
                    if (col_FieldReference == null)
                    {
                        col_Statements.Add(col_VariableDeclaration);
                        col_Statements.Add(col_Assign_Create);
                        col_Statements.Add(col_Assign_data_property_name);
                        col_Statements.Add(col_Assign_header_text);
                        if (col.GetType() == typeof(DataGridViewImageColumn))
                        {
                            col_Statements.Add(col_Assign_image);
                            col_Statements.Add(col_Assign_image_layout);
                        }
                        col_Statements.Add(col_Assign_resizable);
                        col_Statements.Add(col_Assign_visible);
                        col_Statements.Add(col_Assign_width);
                        parameter_list.Add(new CodeVariableReferenceExpression("column" + i.ToString()));
                    }

                    ///

                    statements.AddRange(col_Statements);
                    i++;
                }

                CodeFieldReferenceExpression target = base.SerializeToExpression(manager, value) as CodeFieldReferenceExpression;
                // if the designer hasn't all the columns to the inner DataGridView's column collection, add them by ourselves.
                if (target != null && parameter_list.Count > 0)
                {
                    createArray = new CodeArrayCreateExpression(typeof(DataGridViewColumn), parameter_list.ToArray());
                    methodcall = new CodeMethodInvokeExpression(new CodeVariableReferenceExpression(target.FieldName + ".zColumns"), "AddRange", createArray);

                    statements.Add(methodcall);
                }
            }
        }
        return codeObject;
    }
}

现在,我只是来自:

col_Assign_image = new CodeAssignStatement(new CodeVariableReferenceExpression("column" + i.ToString() + ".Image"), new CodePrimitiveExpression(colImage.Image.ToString()));

当设计师对控件进行序列化时,我得到了以下代码:

System.Windows.Forms.DataGridViewImageColumn column1;
        column1 = new System.Windows.Forms.DataGridViewImageColumn();
        column1.DataPropertyName = "frn_id";
        column1.HeaderText = "";
        column1.Image = "System.Drawing.Bitmap";
        column1.ImageLayout = System.Windows.Forms.DataGridViewImageCellLayout.Stretch;
        column1.Resizable = System.Windows.Forms.DataGridViewTriState.False;
        column1.Visible = true;
        column1.Width = 20;
        this.tableLayoutPanel1.SuspendLayout();
        this.SuspendLayout();

我希望我有类似的东西:

column1.Image = global::....SomePath....Resources.ico_right_arrow;

假设&#39; global :: .... SomePath ....&#39; 是我的资源文件的路径,&#39; ico_right_arrow&#39 ; 我要检索的图像名称(此处为图标)。 任何想法???

0 个答案:

没有答案