11

我正在尝试为InitializeComponent. MSDN 文章“在 .NET Framework 可视化设计器中自定义代码生成”包含“控制代码生成”部分,它解释了如何完成此操作的基础知识。

我密切关注上述文章中的一个示例:

//using System.ComponentModel.Design.Serialization;

class SomeFormSerializer : CodeDomSerializer
{
    public override object Serialize(IDesignerSerializationManager manager,
                                     object value)
    {
        // first, let the default serializer do its work:
        var baseSerializer = (CodeDomSerializer)manager.GetSerializer(
                             typeof(Form).BaseType, typeof(CodeDomSerializer));
        object codeObject = baseSerializer.Serialize(manager, value);

        // then, modify the generated CodeDOM -- add a comment as the 1st line:
        if (codeObject is CodeStatementCollection)
        {
            var statements = (CodeStatementCollection)codeObject;
            statements.Insert(0, new CodeCommentStatement("CODEDOM WAS HERE"));
        }

        // finally, return the modified CodeDOM:
        return codeObject;
    }
}

现在我将它连接到我的表单SomeForm

[DesignerSerializer(typeof(SomeFormSerializer), typeof(CodeDomSerializer))]
class SomeForm : Form { … }

然后,表单设计器可能会生成以下InitializeComponent代码:

private void InitializeComponent()
{
    … /* (general setup code, such as a call to `this.SuspendLayout`) */ 

    // 
    // someButton
    // 
    … /* (someButton's properties are set) */

    // CODEDOM WAS HERE!
    // 
    // SomeForm
    // 
    … /* (form's properties are set) */

    … /* (general setup code, such as a call to `this.ResumeLayout`) */ 
}

请注意,注释// CODEDOM WAS HERE不是作为第一行添加的InitializeComponent,而只是作为处理表单对象本身属性的代码块的第一行。

如果我希望能够修改生成的整个方法的 CodeDOM,而不仅仅是处理特定对象的部分,我该怎么办?

背景:我为什么要这样做?在 Windows 窗体中,如果想要在数据绑定过程中进行灵活的值转换,通常不得不求助于订阅某个特定对象的FormatParse事件。Binding所以我正在创建一个专门Binding的子类(我们称之为它ConvertingBinding)来稍微简化这个过程。

现在,问题是当在 Windows 窗体设计器中设置数据绑定时,生成的代码会创建Binding; 但是,我希望设计器改为实例化我的专门子类。我目前的方法是让设计人员首先创建一个 CodeDOM 树,然后遍历该树并将所有实例替换BindingConvertingBinding.

4

1 回答 1

14

您需要创建两个Form类。首先Form用一个DesignerSerializerAttribute. 第二个Form是第一个的后代。之后,您可以自定义InitializeComponent()第二个Form控件或组件。为此,您应该使用manager.Context来获取包含's 控件的序列StatementContext化 代码的所有对象。CodeStatementCollectionForm

这里有一些简单的步骤。
包括库:

using System.CodeDom;
using System.ComponentModel.Design.Serialization;
using System.Collections;

创建新表单并添加DesignerSerializerAttribute

[DesignerSerializer(typeof(CustomFormSerializer), typeof(CodeDomSerializer))]
class CustomForm : Form { … }

创建CustomForm后代并向其添加一些控件或组件:

class CustomForm1 : CustomForm { … }

CustomFormSerializer为处理添加方法CodeStatementCollection,例如:

private void DoSomethingWith(CodeStatementCollection statements)
{
    statements.Insert(0, new CodeCommentStatement("CODEDOM WAS HERE"));
}

Serialize方法使用循环中manager.Context

public override object Serialize(IDesignerSerializationManager manager,
    object value)
{
    //Cycle through manager.Context            
    for (int iIndex = 0; manager.Context[iIndex] != null; iIndex++)
    {
        object context = manager.Context[iIndex];

        if (context is StatementContext)
        // Get CodeStatementCollection objects from StatementContext
        {
            ObjectStatementCollection objectStatementCollection =
                ((StatementContext)context).StatementCollection;

            // Get each entry in collection.
            foreach (DictionaryEntry dictionaryEntry in objectStatementCollection)
                // dictionaryEntry.Key is control or component contained in CustomForm descendant class
                // dictionartEntry.Value is CodeDOM for this control or component
                if (dictionaryEntry.Value is CodeStatementCollection)
                    DoSomethingWith((CodeStatementCollection)dictionaryEntry.Value);
        }

        //Do something with each collection in manager.Context:
        if (context is CodeStatementCollection)
            DoSomethingWith((CodeStatementCollection)context);
    }

    // Let the default serializer do its work:
    CodeDomSerializer baseClassSerializer = (CodeDomSerializer)manager.
        GetSerializer(value.GetType().BaseType, typeof(CodeDomSerializer));
    object codeObject = baseClassSerializer.Serialize(manager, value);

    // Then, modify the generated CodeDOM:
    if (codeObject is CodeStatementCollection)
        DoSomethingWith((CodeStatementCollection)codeObject);

    // Finally, return the modified CodeDOM:
    return codeObject;
}
于 2012-11-07T09:39:26.523 回答