使用wmfl实现可配置windows窗体
作者: joe stegman
翻译:秋枫
原文出处:http://windowsforms.net/articles/wfml.aspx
文章原名: using the windows forms xml parser sample
代码下载:下载
introduction
这里介绍的是个带有可以扩展机制的例子,通过添加一个标记模型来实现。我们把里面的解析规则可以概述为“xml 元素映射到.net framework 类型 而xml 中的属性映射到类型的属性、方法(或者事件)”。这个例子包含一个markup parser通过解析xml文件来动态的产生一个对象的实例树。对于标记的格式包括下面的结构:
1. xml 命名空间 到 .net framework 命名空间的映射
2. 对象实例化名称
3. 对象验证及引用
4. 属性设置
5. 实例和静态方法
6. 事件委托
7. 组件引用
disclaimer
这个例子中使用的技术不是下一版本windows forms的一部分。另外,这里的例子是针对.net framework version 1.1的,至于其他版本没有测试过。
basic sample
下面的例子显示了一个xml的语法用来声明或定义一个包含了label的简单窗体。
xml version="1.0" encoding="utf-8" ?>
mapping xmlns="http://www.microsoft.com/2003/windowsforms"
namespace="system.windows.forms;system.drawing"?>
<wfml xmlns="http://www.microsoft.com/2003/windowsforms"
xmlns:wfml="http://www.microsoft.com/2003/wfml">
<form wfml:root="true" text="basic sample" size="300,200">
<label text="hello world" autosize="true" location="10, 20"/>
<method.show/>
form>
wfml>
wmfl解析器根据上面的xml文件来动态的生成一个窗体。这里假设上面的xml文件内容包含在“basic.xml”文件中。
markupparser parser = new markupparser();
object form = parser.parse("basic.xml");
下面是动态生成的窗体:
dissecting the basic sample
根据xml文档中的定义,解析器进行了精确的处理,一个根标签,实体声明和一个结束标签。下面是对各部分的描述。
xml declaration
xml version="1.0" encoding="utf-8" ?>
这行定义了xml针对这个例子的默认命名空间同时定义了元素和属性的前缀。
instance declarations
<form wfml:root="true" text="basic sample" size="300,200">
<label text="hello world" autosize="true" location="10,20"/>
<method.show/>
form>
当处理xml的时候,解析器将通过默认构造函数创建一个窗体的实例,同时设置text属性为“basic sample”,size属性为“300,200”。解析器还创建了一个label实例添加到窗体的controls集合,同时设置了label的text属性为“hello world”,设置他的autosize属性为“true”,location属性为“10,20”。最后,解析器还调用了show方法来显示窗体的实例。
xml namespace to .net framework mappings
解析器通过 标签来创建system.windows.forms.form类的一个实例。.net framework 1.1版本至少包含两个不同的“form”类型:一个是在system.windows.forms命名空间,另一个是在system.web.ui.mobilecontrols。解析器通过xml文件中的命名空间映射来选择需要创建的实例的命名空间,在这个例子中,元素使用默认的命名空间。而这里默认的为http://www.microsoft.com/2003/windowsforms:
<wfml xmlns="http://www.microsoft.com/2003/windowsforms" …
这里的“映射”就是指示xml namespace到system.windows.forms .net framework 命名空间:
mapping xmlns="http://www.microsoft.com/2003/windowsforms"
namespace="system.windows.forms;system.drawing"?>
解析器通过这个映射来选择form的类型命名空间为system.windows.forms而不是system.web.ui.mobilecontrols”.
collection heuristics
当解析器遇到元素的子元素时,就会根据上面定义的映射规则来生成相对应的类的实例加入到父控件的一个容器(如果父控件是一个容器,就直接添加)。在默认的情况下,解析器寻找一个“controls”集合但是也可以把子控件通过“.”符号加载到的不同的集合。
<menuitem text="new" >
<property.menuitems>
<menuitem text="window" shortcut="ctrln" />
<menuitem text="-" />
<menuitem text="message" />
<menuitem text="post" />
<menuitem text="contact" />
<menuitem text="internet call" />
property.menuitems>
menuitem>
在上面的例子中,那些子菜单被明确的加入到父一级的menuitem的“menuitems”集合中。对于“property.propertyname”符号在下面的markup reference一节中会有更多的详细说明。
markup reference
这个简单的解析器使用一些的属性,元素和一些其他符号指令来处理转换。那些属性和元素在http://www.microsoft.com/2003/wfml命名空间下,同时使用“wfml”作为前缀。
wfml:id attribute
这个id属性用来唯一表示元素的实例。另外,解析器还提供一个api通过id来查询一个实例,在下面的例子中,form元素被给了“form1”。
xml version="1.0" encoding="utf-8" ?>
mapping xmlns="http://www.microsoft.com/2003/windowsforms"
namespace="system.windows.forms;system.drawing"?>
<wfml xmlns="http://www.microsoft.com/2003/windowsforms"
xmlns:wfml="http://www.microsoft.com/2003/wfml">
<form wfml:id="form1" text="basic sample" size="300,200">
<label text="hello world" autosize="true" location="10, 20"/>
<method.show/>
form>
wfml>
解析器能通过“find”方法来重新获取“form1”实例。
markupparser parser = new markupparser();
parser.parse(“basic.xml”);
form form1 =(form)parser.find(“form1”);
wfml:root attribute
root属性标识由解析器的“parser”方法生成的实例。整个xml只能有一个元素使用root属性。下面的例子中“form1”元素包含了“root”属性。
xml version="1.0" encoding="utf-8" ?>
mapping xmlns="http://www.microsoft.com/2003/windowsforms"
namespace="system.windows.forms;system.drawing"?>
<wfml xmlns="http://www.microsoft.com/2003/windowsforms"
xmlns:wfml="http://www.microsoft.com/2003/wfml">
<form wfml:id="form1" wfml:root="true" text="basic sample" size="300,200">
<label text="hello world" autosize="true" location="10, 20"/>
<method.show/>
form>
wfml>
下面是“parse”方法返回一个“form1”实例:
markupparser parser = new markupparser();
form form1 = (form)parser.parse(“basic.xml”);
wfml:argument attribute
当实例化类型的时侯,解析器使用类型的默认构造函数。但是有些类型没有默认的构造函数,比如bitmap。
argument属性专门为没有默认构造函数的类型传递一个参数。下面的例子就是为bitmap的构造函数传递一个“c:\image.jpg”参数。
<form wfml:root="true" wfml:id="form1" name="form1" text="set background">
<property.backgroundimage>
<bitmap wfml:argument="c:\\image.jpg"/>
property.backgroundimage>
<method.show/>
form>
注意这里如果是使用带多个参数的构造函数是不可能的。
property element
解析器的通常规则是xml元素映射到类型而xml属性映射到类型的属性。
<element attribute="string value"/>
解析器能通过typeconverters(system.componentmodel.typeconverter)来有效的处理字符串到非字符串属性的转换。举例form类的backcolor属性是“system.drawing.color”类型的,但是可以使用下面的设置:
<form backcolor="green"/>
在上面的例子中,backcolor属性是通过一个typeconverter来把“green”字符串转换成“system.drawing.color”得到的。在一般情况下typecoverter模块可以很正常的工作,但是在处理复杂的属性类型时(比如image)不会提供一个很好的结果。解析器通过使用一个“.”属性符号来处理更多复杂属性的设置,下面使用就是设置窗体的backgroundimage:
<form wfml:root="true" wfml:id="form1" name="form1" text="set background">
<property.backgroundimage>
<bitmap wfml:argument="c:\\image.jpg"/>
property.backgroundimage>
<method.show/>
form>
在上面的例子中,把bitmap实例作为窗体的backgroundimage设置。
method element
类似于上面的“.”属性符号,解析支持“.”方法符号。当解析器发现一个元素带有“method.methodname”,他将会在父元素实例中调用“methodname”方法。下面就会在“form1”实例中调用“show”方法。
<form wfml:root="true" wfml:id="form1" name="form1" text="set background">
<property.backgroundimage>
<bitmap wfml:argument="c:\\image.jpg"/>
property.backgroundimage>
<method.show/>
form>
解析器能调用实例或静态函数,但是不能调用含有多个参数的方法。下面的例子是调用带有一个参数的静态方法:
<form wfml:root="true" wfml:id="form1" name="form1" text="set background">
<property.backgroundimage>
<method.fromfile static="system.drawing.image">
c:\\image.jpg
method.fromfile>
property.backgroundimage>
<method.show/>
form>
上面调用静态方法“system.drawing.image.fromfile”,参数为“c:\image.jpg”。窗体的的backgroundimage属性值为静态方法的返回值。
wfml:var element
解析器对创建引用类型的变量提供了有限的支持。任何属性使用wfml:id 属性能被解析为引用类型。如下:
<form wfml:id="form1" name="form1" text="simple"/>
<wfml:var name="form1">
<method.show/>
wfml:var>
在上面的例子中,“wfml:var”元素引用“form1”参数也就是窗体,“show”方法在“form1”的实例中被调用。
variable prefix
以“$”开始的属性值被处理为是引用参数。
<wfml:var wfml:id="image">
<method.fromfile static="system.drawing.image">c:\\image.jpgmethod.fromfile>
wfml:var>
<form backgroundimage="$image" name="form1" text="set background">
<method.show/>
form>
mapping processing instruction
通常的使用语法是:
mapping xmlns="xmlnamespace" namespace=".net namespaces"?>
.net framework命名空间属性可以包含一个或多个用“;”隔开的列表。
mapping xmlns="mynamespace" namespace="system.windows.forms;system.drawing"?>
在实例化类型的时侯,解析器将会映射xml元素中指定的“xmlns”到.net framework类型。
assembly processing instruction
对于“assembly”的处理指令是通过添加装配件引用。
assembly name="system.data" version="1.0.5000.0" culture="neutral" publickeytoken="b77a5c561934e089"?>
markup parser reference
解析器根据xml文件动态的生成一个实例树,而他的api描述如下:
parse 方法使用一个字符串参数或者一个textreader作为参数。该方法返回的一个实例是文档里那个包含了“wfml:root”的属性的元素的id值,或者就是第一个元素。
addvariable method
下面的例子将产生一个窗体同时包装窗体的load事件:
<form wfml:root="true" load="me.formloadhandler" text="load sample"/>
解析器会认出“load”是一个事件并寻找一个“me”的变量同时把窗体的load事件用“me’s”的formloadhandler委托来包装。
private void button1_click(object sender, system.eventargs e)
{
markupparser parser = new markupparser();
// add "this"
parser.addvariable("me", this);
// parse
object obj = parser.parse("sample.xml");
}
private void formloadhandler(object sender, eventargs e)
{
messagebox.show("form load");
}
find method
find方法被用来从变量上下文中重新获得一个实例(wfml:id)。举例,下面的例子定义了一个“form1”窗体,里面包含了一个“button1”按钮:
<form wfml:root="true" wfml:id="form1" text="find sample">
<button wfml:id="button1"/>
form>
通过find方法能够重新得到“button1”的实例:
private void button1_click(object sender, system.eventargs e)
{
markupparser parser = new markupparser();
// parse (returns form1)
object obj = parser.parse("sample.xml");
// get button1 instance
button button1 = parser.find("button1") as button;
}
reset method
解析器的变量上下文(变量列表)是跨多个解析器实例共享的(静态列表)。
reset方法就是用来清空这个共享变量列表。
status event
解析器的状态事件提供每一行的解析信息。注意这个事件只有在调试场景下才有用。
