3
我已經創建了一個組件來爲我們的項目進行本地化。我遇到的問題是我輸出的代碼被注入到InitializeComponent中發生得太晚了。CodeDomSerializer輸出訂購
例
this.components = new System.ComponentModel.Container();
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(ucHome));
this.localizationResourceManager1 = new Compass0.Localization.LocalizationResourceManager(this.components);
//Other designer code to create controls (this is all in the first block of code in InitializeComponent
Compass0.Localization.XMLResourceManager.Create(typeof(ucViewHome), ref resources); //created by my code serializier
//
// btnLogin
//
等等等等
但什麼是由設計者情況是
this.components = new System.ComponentModel.Container();
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(ucHome));
this.btnLogin = new ComponentFactory.Krypton.Toolkit.KryptonButton();
this.localizationResourceManager1 = new Compass0.Localization.LocalizationResourceManager(this.components);
//Other designer code to create controls (this is all in the first block of code in InitializeComponent
//
// btnLogin
//
this. btnLogin.Name = "btnLogin";
this. btnLogin.Text = resources.GetString("btnLogin.Text");
Compass0.Localization.XMLResourceManager.Create(typeof(ucViewHome), ref resources); //created by my code serializier
基本上,我需要迫使設計師把我的代碼後的3號線資源已經初始化。有沒有辦法以直接的方式做到這一點?我的代碼生成類如下。
public override object Deserialize(IDesignerSerializationManager manager, object codeDomObject)
{
CodeDomSerializer baseSerializer = (CodeDomSerializer)manager.GetSerializer(typeof(LocalizationResourceManager).BaseType, typeof(CodeDomSerializer));
return baseSerializer.Deserialize(manager, codeDomObject);
}
public override object Serialize(IDesignerSerializationManager manager, object value)
{
CodeDomSerializer baseSerializer = (CodeDomSerializer)manager.GetSerializer(typeof(LocalizationResourceManager).
BaseType, typeof(CodeDomSerializer));
object codeObject = baseSerializer.Serialize(manager, value);
if (codeObject is CodeStatementCollection)
{
CodeStatementCollection statements = (CodeStatementCollection)codeObject;
CodeTypeDeclaration classTypeDeclaration = (CodeTypeDeclaration)manager.GetService(typeof(CodeTypeDeclaration));
CodeExpression typeofExpression = new CodeTypeOfExpression(classTypeDeclaration.Name);
CodeDirectionExpression resourceRef = new CodeDirectionExpression(FieldDirection.Ref, new CodeVariableReferenceExpression("resources"));
CodeExpression ResourceManagerAssignment = new CodeMethodInvokeExpression(
new CodeTypeReferenceExpression(typeof(XMLResourceManager).ToString()),
"Create", new CodeExpression[] { typeofExpression, resourceRef });
statements.Insert(0, new CodeExpressionStatement(ResourceManagerAssignment));
}
return codeObject;
}