原文:http://hi.baidu.com/xletian/blog/item/2d7e7ac6aaab0e0b9d163d82.html

XUL(XML User-interface Language)是firefox的界面描述语言,事实上Mozilla的大部分产品都是用XUL作界面,其中XULRunner还能以XUL为界面来 开发应用;在上一节中提到,在chrome.manifest中定义了一个覆盖(overlay),把overlay.xul合并到主界面,在工具菜单上 增加一项,现在来看overlay.xul的代码:

<?xml version="1.0"?>
<?xml-stylesheet href="chrome://helloworld/skin/overlay.css" type="text/css"?>
引入css文件overlay.css,菜单被定义为红色;
<!DOCTYPE overlay SYSTEM "chrome://helloworld/locale/overlay.dtd">
使用相应的本地化的overlay.dtd代替以&开始的内容;
<overlay id="helloworld-overlay"
xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
overlay节点就是被附加的内容;
<script src="overlay.js"/>
引入js文件
<menupopup id="menu_ToolsPopup">
“合并点”,和主界面中相同的内容被合并;
<menuitem id="helloworld-hello" label="&helloworld;"
oncommand="HelloWorld.onMenuItemCommand(event);"/>
具体的菜单,label属性为显示的文字,oncommand属性为点击事件,HelloWorld.onMenuItemCommand函数定义在overlay.js中。
</menupopup>
</overlay>

再来看overlay.js:
[code=javascript]
var HelloWorld = {
onLoad: function() {
// initialization code
this.initialized = true;
},
onMenuItemCommand: function() {
window.open("chrome://helloworld/content/hello.xul", "", "chrome");
}
};
window.addEventListener("load", function(e) { HelloWorld.onLoad(e); }, false);
[/code]

HelloWorld.onMenuItemCommand函数只做了一件事:打开chrome://helloworld/content/hello.xul文件:

<?xml version="1.0"?>
<?xml-stylesheet href="chrome://global/skin/global.css" type="text/css"?>
<!DOCTYPE window SYSTEM "chrome://helloworld/locale/hello.dtd">
<window xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
title="&title.label;">
<hbox align="center">
<description flex="1">&separate.label;</description>
<button label="&close.label;" oncommand="close();"/>
</hbox>
</window>

这是个独立的窗口,所以没有overlay节点,代之以window节点。

XUL文件中也可以写XHTML代码,方法是引入XHTML名字空间(在window节点增加属性
xmlns:html="http://www.w3.org/1999/xhtml")
另外所有的XHTML标签都要加html:,比如<p>要写成<html:p>。给上面的例子加点xhtml代码:

<?xml version="1.0"?>
<?xml-stylesheet href="chrome://global/skin/global.css" type="text/css"?>
<!DOCTYPE window SYSTEM "chrome://helloworld/locale/hello.dtd">
<window xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
xmlns:html="http://www.w3.org/1999/xhtml"
title="&title.label;">
<html:p>这是html代码</html:p>
<hbox align="center">
<description flex="1">&separate.label;</description>
<button label="&close.label;" oncommand="close();"/>
</hbox>
</window>

转:http://fregen.yo2.cn/articles/xul-basic.html

posted on 2011-11-02 14:04  hotty  阅读(669)  评论(0)    收藏  举报