English 中文(简体)
如何动态加载CSS文件到Flex应用程序中?
原标题:
  • 时间:2008-10-15 14:27:58
  •  标签:

I know that you can apply CSS in order to style objects in Flex using the StyleManager:
http://livedocs.adobe.com/flex/3/html/help.html?content=styles_07.html

You can also load compiled CSS files (SWFs) dynamically:
http://livedocs.adobe.com/flex/3/html/help.html?content=styles_10.html

However, I m dynamically creating my CSS files using a web GUI and a server-side script. If the CSS is changed, then the script would also need to compile the CSS into an SWF (which is not a viable option). Is there any way around this?

最佳回答

CSS 在 Flex 中的应用在编译时在服务器端处理,而不是在运行时在客户端处理。

我会为你看到两个选项(但我不确定它们有多实际):

  1. Use a server side script to compile your CSS as a SWF then load them dynamically.
  2. Parse a CSS Style sheet and use the setStyle functions in flex to apply the styles. An similar example to this approach is the Flex Style Explorer where you can check out the source.

祝你好运。

问题回答

在Adobe错误跟踪器中,与此相关的问题中的这个评论中,T. Busser正在描述可能是您的可行解决方案:

"I ve created a small class that will parse a CSS file read with an HTTPService object. It takes apart the string that is returned by the HTTPService object, break it down into selectors, and create a new CSSStyleDeclaration object for each selector that is found. Once all the properties are assigned to the CSSStyleDeclaration object it s added to the StyleManager. It doesn t perform any checks, you should make sure the CSS file is well formed, but it will be sufficient 99% of the time. Stuff like @font, Embed() and ClassReference() will hardly be used by my customers. They do need the ability to change colors and stuff like that so they can easily theme the Flex application to their house style."

你可以尝试联系此人以获取他们的解决方案,或者也可以使用as3csslib项目中的代码作为编写类似于他们所描述的东西的基础。

You can also implement dynamic stylesheet in flex like this . Here i found this article : http://askmeflash.com/article_m.php?p=article&id=6

编辑:此解决方案无效。从解析器中去掉的所有选择器都会转换为小写。这对您的应用程序可能有效,但可能不会...

我把这个答案留在这里是因为它可能会帮助一些正在寻找解决方案的人,并警告其他人这种方法的局限性。


请看我的问题:“寻找用AS3编写的CSS分析器”进行全面讨论,但我发现了一个隐藏在标准库中的CSS分析器。这是如何使用它的:

public function extractFromStyleSheet(css:String):void {

    // Create a StyleSheet Object
    var styleSheet:StyleSheet = new StyleSheet();
    styleSheet.parseCSS(css);

    // Iterate through the selector objects
    var selectorNames:Array = styleSheet.styleNames;
    for(var i:int=0; i<selectorNames.length; i++){

        // Do something with each selector
        trace("Selector: "+selelectorNames[i];

        var properties:Object = styleSheet.getStyle(selectorNames[i]);
        for (var property:String in properties){

            // Do something with each property in the selector
            trace("	"+property+" -> "+properties[property]+"
");
        }
    }
}

您可以使用以下方式应用样式:

cssStyle = new CSSStyleDeclaration();
cssStyle.setStyle("color", "<valid color>);
FlexGlobals.topLevelApplication.styleManager.setStyleDeclaration("Button", cssStyle, true);




相关问题
热门标签