如何将 CSS 文件动态加载到 Flex 应用程序中?

发布于 2024-07-08 09:38:59 字数 587 浏览 5 评论 0原文

我知道您可以应用 CSS,以便使用 StyleManager 在 Flex 中设置对象的样式:
http://livedocs.adobe.com/flex/ 3/html/help.html?content=styles_07.html

您还可以动态加载已编译 CSS 文件 (SWF):
http://livedocs.adobe.com/flex/ 3/html/help.html?content=styles_10.html

但是,我使用 Web GUI 和服务器端脚本动态创建 CSS 文件。 如果 CSS 发生更改,则脚本还需要将 CSS 编译为 SWF(这不是一个可行的选项)。 有没有办法解决?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

萤火眠眠 2024-07-15 09:38:59

此评论中,针对 Adob​​e 错误跟踪器中与此相关的问题T. Busser 正在描述对您来说可能可行的解决方案:

“我创建了一个小类,它将‘解析’用
HTTPService 对象。 它拆开
返回的字符串
HTTPService对象,将其分解为
选择器,并创建一个新的
每个 CSSStyleDeclaration 对象
找到的选择器。 一旦所有
属性被分配给
添加的 CSSStyleDeclaration 对象
到样式管理器。 事实并非如此
执行任何检查,您应该进行
确保 CSS 文件格式正确,但是
99% 的情况下就足够了。
像 @font、Embed() 和
ClassReference() 几乎不会被使用
由我的客户。 他们确实需要
改变颜色和东西的能力
这样他们就可以轻松地主题化
Flex 应用程序到他们的房子
风格。”

您可以尝试联系此人寻求解决方案,也可以使用 这个 as3csslib 项目 作为编写类似他们所描述的内容的基础。

In this comment to an issue related to this in the Adobe bug tracker T. Busser is describing what might be a viable solution for you:

"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."

You could either try to contact this person for their solution or alternatively maybe use the code from this as3csslib project as a basis for writing something like what they're describing.

苦笑流年记忆 2024-07-15 09:38:59

编辑:此解决方案不起作用。 从解析器中取出的所有选择器都将转换为小写。 这可能适用于您的应用程序,但可能不会...

我将这个答案留在这里,因为它可能会帮助某些人寻找解决方案并警告其他人此方法的局限性。 >


请参阅我的问题:“寻找用 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("\t"+property+" -> "+properties[property]+"\n");
        }
    }
}

然后您可以使用以下方法应用样式:

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

Edit: This solution does not work. All selectors that are taken out of the parser are converted to lowercase. This may work for your application but it will probably not...

I am leaving this answer here because it may help some people looking for a solution and warn others of the limitations of this method.


See my question: "Looking for CSS parser written in AS3" for a complete discussion but I found a CSS parser hidden inside the standard libraries. Here is how you can use it:

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("\t"+property+" -> "+properties[property]+"\n");
        }
    }
}

You can then apply the styles using:

cssStyle = new CSSStyleDeclaration();
cssStyle.setStyle("color", "<valid color>);
FlexGlobals.topLevelApplication.styleManager.setStyleDeclaration("Button", cssStyle, true);
楠木可依 2024-07-15 09:38:59

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

我会为您看到两个选项(我不确定这两个选项有多实用):

  1. 使用服务器端脚本将 CSS 编译为 SWF,然后动态加载它们。
  2. 解析 CSS 样式表并使用 flex 中的 setStyle 函数来应用样式。 与此方法类似的示例是 Flex Style Explorer,您可以在其中查看来源

祝你好运。

The application of CSS in Flex is handled on the server side at compilation and not on the client side at run time.

I would see two options then for you (I'm not sure how practical either are):

  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.

Good luck.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文