从外部资源设置 Silverlight 应用程序的配色方案
我有一个包含六个自定义用户控件的 Silverlight 3 应用程序。我想从外部资源加载这些控件的配色方案。
包含默认配色方案的代码和 XAML 将构建在 XAP 中。然后,对象标签上的参数将包含一个 URL,可以从该 URL 动态加载替代颜色。
顺便说一句,如果可能的话,可以使用 Silverlight 3 应用程序主题功能,但确实有点过分了。只需要改变颜色。
这可能吗?您建议如何实现?
I have a Silverlight 3 application containing six custom user controls. I'd like to load the colour scheme for these controls from an external resource.
The code and XAML containing a default colour scheme would be built in the XAP. Then a parameter on the object tag would contain a URL from where alternate colours can be dynamically loaded.
By the way, the Silverlight 3 application theme feature could be used if that's possible but is really overkill. Only colours need to be changed.
Is this possible and how would you recommend to do it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我将这样做。
在 App.xaml 中,我将像这样定义应用程序资源字典:-
现在,我将 ColorTable.xaml 放置在 XAP 之外的 XAP 所在的同一文件夹中。这并不完全符合您的所有标准,因为始终需要外部 ColorTable。可以稍微调整一下以达到完整的要求,但相比之下会很混乱。
Here is how I would do it.
In App.xaml I would define the application Resource dictionary like this:-
Now I would place the ColorTable.xaml outside of the XAP in the same folder that the XAP is sited. This doesn't quite meet all your criteria since an external ColorTable is always required. It is possible to flex this somewhat to achieve the full requirement but it'll be quite messy in comparison.
我会看一下 Corinna Barber 在这两篇文章中使用的技术:
http://blogs.msdn.com/corrinab/archive/ 2009/11/24/9927729.aspx
http://blogs.msdn.com/corrinab/archive/ 2009/12/02/9931283.aspx
基本上,她所做的是,在应用程序启动时,她在绑定帮助器类(她的类称为 SysColors)中创建一堆画笔(实体和渐变)。然后她只需绑定到这些画笔,如下所示:
Background="{Binding CalendarGradient, Source={StaticResource SysColors}}"
她的方法的主要缺点是您必须编写大量代码创建渐变画笔时。所有不同的梯度停止点都必须独立存储在数据库(或 xml 或其他)中。我现在想,您可以将画笔存储为 xaml,然后使用 XamlReader.Load 立即加载整个画笔对象。这对我来说听起来是一个更好的计划,但我还没有尝试过,我只是大声思考。
在您的情况下,在应用程序启动时,您可以轻松加载默认方案或从 WCF 服务或其他地方获取颜色值。您甚至可以为所有画笔实现 INotiyPropertyChanged,从而能够在运行时交换它们。但我想这可能会给你带来糟糕的表现。
I would take a look at the technique Corinna Barber uses in these two articles:
http://blogs.msdn.com/corrinab/archive/2009/11/24/9927729.aspx
http://blogs.msdn.com/corrinab/archive/2009/12/02/9931283.aspx
Basically what she does is, at application startup, she creates a bunch of brushes (both solid and gradients) in a binding helper class (hers is called SysColors). Then she simply binds to these brushes, like so:
Background="{Binding CalendarGradient, Source={StaticResource SysColors}}"
The main downside to her approach is that you have to write quite a bit of code when creating gradient brushes. And all the different gradient stops would have to be stored independently in your database (or xml or whatever). I'm thinking now that you could probably store your brushes as xaml and just use
XamlReader.Load
to load the entire brush object at once. That sounds like a better plan to me, but I haven't tried this, I'm just thinking out loud.In your situation, at application startup, you could easily load your default scheme OR pick up your color values from a WCF service or wherever. You could even implement INotiyPropertyChanged for all the brushes, and thus be able to swap them at runtime. But I guess that might give you bad performance.