根据不同的XAML样式继承 [英] Style inheritance based on different XAML

查看:100
本文介绍了根据不同的XAML样式继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在风格指定支持算法FMP标签在其他文件中定义的样式。

How to specify BasedOn tag in a style to a style defined in some other file.

例如:

Dictionary1.xaml定义

Dictionary1.xaml defines

   <Style x:Key="basicStyle" TargetType="TextBlock" >
       <Setter Property="FontSize" Value="24"></Setter>
       <Setter Property="Foreground" Value="DarkGray"></Setter>
       <Setter Property="FontWeight" Value="Bold"></Setter>
    </Style>

在Dictionary2.xaml我需要这样的东西

In Dictionary2.xaml i need something like

    <Style x:Key="headerStyle" TargetType="TextBlock" >
       <Setter Property="FontSize" Value="46"></Setter>
       <Setter Property="Foreground" Value="DarkGray"></Setter>
       <Setter Property="FontWeight" Value="Bold"></Setter>
    </Style>

如何实现这一目标?

How to achieve this?

推荐答案

最简单的方法:

Dictionary2.xaml (开幕的ResourceDictionary 标签之后)定义MergedDictionaries:

In Dictionary2.xaml define MergedDictionaries (right after the opening ResourceDictionary tag):

<ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="/Path/to/Dictionary1.xaml" />
</ResourceDictionary.MergedDictionaries>

然后

<Style x:Key="headerStyle" TargetType="TextBlock" BasedOn="{StaticResource basicStyle}" >
    .....
</Style>

这将解决这一问题,但是与所有容易解决方案有一个问题:每次合并词典时候你有效地创建合并字典的一个副本。而且它是递归的 - 如果你有Dict3.xaml和Dict4.xaml这两个负载Dictionary2.xaml,你将有创建Dictionary1.xaml的三个实例。由于复杂的依赖关系结构,你可以到一个地步,你必须在内存19,000+字典对象的应用程序启动和内存占用变从180MB到1200MB(TrueStory™:()。

This will solve the issue, but as with all easy solutions there's a catch: each time you merge dictionaries you effectively create a copy of the merged dictionary. And it's recursive - if you have Dict3.xaml and Dict4.xaml that both load Dictionary2.xaml, you will have three instances of Dictionary1.xaml created. With a complex dependency structure you can get to a point that you have 19,000+ dictionary objects in memory at application start up and the memory footprint goes from 180MB to 1200MB (TrueStory™ :( ).

该解决方案是一个 SharedResourceDictionary 。本教程中的实现应该被看作是一个起点,可能需要调整一些水平 - 根据使用场景。谷歌WPF SharedResourceDictionary一些陷阱和解决方案。

The solution is a SharedResourceDictionary. The implementation in the tutorial should be seen as a starting point and will probably need some level of tweaking - depending on use scenario. Google "wpf SharedResourceDictionary" for some gotchas and solutions.

这篇关于根据不同的XAML样式继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆