wpf中的样式继承表现
假设我有 10000 个样式相当丰富的 WPF 控件,并且它们的样式大约 90% 相同。
我想知道:从性能角度来看,最好的方法是什么?
- 有 10000 个独特的样式
- 有 1 个样式包含常见的 90% + 10000 个样式继承此样式并添加剩余的 10%
我毫不犹豫地选择了第二个选项(出于可读性考虑),但我做了一些基本测试,这些测试倾向于表明这在性能方面是一个非常糟糕的主意。那么我的测试是否有问题,或者样式继承真的会降低性能吗?
编辑:好吧,我几乎可以肯定这没有得到足够好的解释。我将尝试详细说明一下:
基本上,我关心的是 DataGrid。 我的单元格是高度可定制的(基本上,您可以更改单元格中的每个样式属性),并且由于数据网格不允许轻松完成此操作(由于数据网格单元格的数据上下文是 rowView 而不是 CellView),我做了一些事情实现我添加要做的事情真的很难看(如果您想了解详细信息,请参阅那里)。它基本上包括在每个单元格的代码隐藏中设置 CellStyle。
由于这些样式有很多共同点,我尝试设置一个通用样式,放入我的应用程序的静态资源,并将所有单元格样式都基于这个通用样式。 (除其他外,这种样式托管事件侦听器,而 cellStyles 添加了对诸如前景颜色、字体大小等属性的绑定...)
当我尝试这样做时,我注意到数据网格的性能大幅下降。 现在,由于我当时并没有真正测试应用程序的这一侧,因此可能只是代码的某些部分写得很糟糕,并且性能的下降与我使用“basedon”样式无关。不管怎样,我回滚并完成了正在进行的测试。
现在,我正在整理我的代码,并再次考虑这一点。 在再次麻烦地更改我的所有样式结构之前,如果只是为了测试目的,我想我会先在这里问是否值得麻烦...没有更多的事情发生...
感谢您的帮助!
Let's say I have 10000 WPF controls that are quite heavily styled, and that their styles are about 90% identical.
I'm wondering: what would be the best way to do, performance-wise?
- have 10000 unique styles
- have 1 style that contains the common 90% + 10000 styles inheriting this style and adding the remaining 10%
I had gone for the second option without second thoughts (on readability considerations), but I made some basic tests that tended to show this was a very bad idea in terms of performance. So are my tests buggy, or does Style-inheritance really lower perfs?
edit: okay, I was almost certain that this was not explained well enough. I'll try to detail a bit:
basically, My concern lays with the DataGrid.
My cells are heavily customisable (basically, you can change every style property in a cell), and as the datagrid does not allow this to be done easily (thanks to the datagridcell's datacontext being the rowView instead of the CellView), I have done something really ugly to achieve what I add to do (see there if you want the detail). It basically consists in setting the CellStyle in code-behind for each cell.
as those styles have a lot in common, I tried setting up a common style, put in my app's static resources and base all my cell styles on this common style. (this style hosted event listeners, amongst other things, while the cellStyles added the bindings to properties such as the foreground Color, FontSize, etc...)
I noticed a big drop in the datagrid's perf when I tried that.
Now, as I was not really testing this side of the app at the time, it might just have been that some part of the code was simply badly written and the drop in the perf had nothing to do with me using "basedon" styles. Anyway, I rolledBack and finished the testing I had going on.
Now, I'm tidying my code and I though about this again.
Before going to te trouble of changing all my style-structure again, if anly just for test-purposes, I thought I'd ask here first if it was worth the trouble... nothing more going on...
thanks for your help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想说肯定是后者。将共性归结为“可继承”的风格可能是最好的。至少在运行时加载这些样式在磁盘时间方面会更快。即使每个子样式都有一堆 setter,但由于数量如此之多,与相当于同一控件的 10,000 个完全指定的样式相比,读取时间会更少。尽管还有另外 10% 的差异,但有没有一种方法可以将这些差异也归结为风格集?
您可以拥有基于样式的样式,而样式又基于样式...
此外,您是否可以将某些内容推送到这些项目上的触发器?假设您的所有样式设置有 10000 种可能的组合。如果您可以将其中的一些移至多个触发器,似乎会减少您需要的样式选择的数量。
I would say definitely the latter. Boiling commonality down to "inheritable" styles would probably be the best. At the very least loading those styles at runtime would be faster in regards to disk time. Even if each sub style had a bunch of setters, with so many it would take less reading time that 10,000 fully specified styles that amount to the same control. Although with the other 10% of differentiation, is there a way you can boil those differences into sets of styles also?
You can have a style that is based on a style that is based on a style...
Additionally, are there some things that you can push off to Triggers on these items? Lets say you have 10000 possible combinations of all of your style settings. If you could move off a few of those to several triggers, seems like it would lower the number of style choices that you would need.