为什么在 WPF 中将资源放入 APP.xaml 中比放入 MyClass.xaml 中更快?
事情是这样的:
我有一个项目,其中我的菜单/ContextMenu/MenuItems 是模板化的。
所以基本上我有一个 MyMenuItem.xaml/MyMenuItem.xaml.cs 文件以及包含一些资源+样式和模板的 MyMenuItem.xaml 文件。
问题:我的 contextMenu (我承认相当大,但仍然......)大约需要 14 秒来加载(!!)
我做了一些分析并发现这一行: MyMenuItem item = new MyMenuItem();
这 14 秒花费了 65% 的 UC。因此,我尝试删除 MyMenuItem.xaml 中的样式、模板和资源,事实上,我加载菜单的时间减少到了 0.3 秒。然后我将这些样式/模板放入 App.xaml 中,并设法获得我想要的设计,但具有预期的性能。
我的问题是:怎么会呢?
为什么我无法将 menuItem 样式放入 MyMenuItem.xaml 文件中,而必须将它们放入 App.xaml 中才能获得良好的性能?我不明白这里发生了什么?
沿着这条路走下去,我推断出我的其他控件的所有其他样式实际上可能有相同的问题,所以我应该将所有模板/样式放在 App.xaml 中,这违背了每个控件的目的控制 .xaml/.xaml.cs 夫妇
我在这里错过/做错了什么?
编辑:以防万一,这是我的 MyMenuItem.xaml 文件的模型:
<MyMenuItem ...>
<MyMenuItem.Resources>
<Style x:Key="Style1" />
<Style x:Key="Style2" />
<Style x:Key="Style3" />
</MyMenuItem.Resources>
<MyMenuItem.Style>
<Style TargetType="MenuItem">
<Setter Property=Template>
<Setter.Value>
<ControlTemplate TargetType="MenuItem">
<!--here I'm using Styles 1,2 and 3-->
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</MyMenuItem.Style>
</MyMenuItem>
问题出在 MyMenuItem.Resources
中的所有内容(即:样式 1,2 和3)
here's the thing:
I have a project in which my menus/ContextMenu/MenuItems are templated.
so basically I have a MyMenuItem.xaml/MyMenuItem.xaml.cs file couple with my MyMenuItem.xaml file containing some resources + style and template.
problem: my contextMenu (quite huge I admit, but still...) took about 14sec to load (!!)
I made some profiling and discovered that this line: MyMenuItem item = new MyMenuItem();
took 65% of the UC for those 14sec. So I tried getting rid of my styles, templates and resources in MyMenuItem.xaml and indeed, I got down to 0.3sec to load the menu. So then I put those Styles/templates in App.xaml, and managed to get the design as I wanted, but with the expected performance.
my question is: how come?
how come I cannot put my menuItem styles in my MyMenuItem.xaml file and have to put them in App.xaml to get good perf? what is going on here than I do not inderstand?
going further down this road, I deduce that all my other styles for my other controls may actually have the same issue, so then I should then put all my templates/styles in App.xaml, which kind of defeats the purpose of having for each control a .xaml/.xaml.cs couple
what am I missing/doing wrong here?
edit: just in case, here is a mockup of my MyMenuItem.xaml file:
<MyMenuItem ...>
<MyMenuItem.Resources>
<Style x:Key="Style1" />
<Style x:Key="Style2" />
<Style x:Key="Style3" />
</MyMenuItem.Resources>
<MyMenuItem.Style>
<Style TargetType="MenuItem">
<Setter Property=Template>
<Setter.Value>
<ControlTemplate TargetType="MenuItem">
<!--here I'm using Styles 1,2 and 3-->
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</MyMenuItem.Style>
</MyMenuItem>
and the issue is with everything including in MyMenuItem.Resources
(i.e: styles 1,2 and 3)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您在菜单级别声明资源,将为菜单的每个实例创建这些资源。在应用程序级别声明它,并将创建资源的单个实例并在所有菜单之间共享。基本上,您希望将资源尽可能地移动到逻辑树上,以便最大限度地共享这些资源。
也就是说,14s 听起来太多了,所以我只能假设你在那里有一些相当重的资源。
If you declare your resources at the menu level, those resources will be created for every instance of the menu. Declare it at the app level, and a single instance of the resources will be created and shared across all menus. Basically, you want to move resources as far up the logical tree as makes sense so that you maximize the sharing of those resources.
That said, 14s sounds excessive so I can only assume you've got some pretty heavy resources in there.