问题:
使用样式表(css)。如果在mxml中使用<fx:style>标签,即<fx:style>标签必需要mxml文档根元素的直接子元素。在给Alert设置样式时报错:必须使用命名空间限定 CSS 选择器“Alert”中的类型“Alert”。
分析:
常用的css选择器有class选择器和type选择器。使用type选择器时需要使用命名空间,语法如下:@namespace s "library://ns.adobe.com/flex/spark"; s|Button{}。
解决实例:
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600">
<fx:Declarations>
<!-- 将非可视元素(例如服务、值对象)放在此处 -->
</fx:Declarations>
<!-- 设定应用样式 -->
<fx:Style>
@namespace mx "library://ns.adobe.com/flex/mx"; mx|Alert{
fontSize: 18pt;
}
@namespace s "library://ns.adobe.com/flex/spark"; s|Button{
fontSize: 18pt;
}
@namespace s "library://ns.adobe.com/flex/spark"; s|TextInput{
fontSize: 18pt;
}
</fx:Style>
<fx:Script>
<![CDATA[
//导入Alert类和关闭事件类
import mx.controls.Alert;
import mx.events.CloseEvent;
//定义私有函数弹出对话框
private function popAlert():void{
//修改Alert.YES和Alert.NO的显示文本
Alert.yesLabel = "是";
Alert.noLabel = "否";
//弹出对话框设定显示标题为“选择对话框”,提示内容为“请选择'是'或者'否'”,显示Alert.YES和Alert.NO两个按钮
//设定关闭Alert对话框响应的事件,设定缺省按钮为Alert.YES
Alert.show("请选择'是'或者'否'","选择对话框",Alert.YES|Alert.NO,this,clickSelectHandler,null,1);
}
//定义私有函数响应用户对Alert对话框的操作
private function clickSelectHandler(event:CloseEvent):void{
//得到用户点击的按钮
if (event.detail==Alert.YES)
//在TextInput组件当中显示的内容
displaySelect_txt.text="您选择了是";
else
displaySelect_txt.text="您选择了否";
}
]]>
</fx:Script>
<s:Button id="popAlert_btn" y="87" label="弹出对话框" horizontalCenter="0" click="popAlert()"/>
<s:TextInput id="displaySelect_txt" y="144" horizontalCenter="0"/>
</s:Application>