开发者

Why does styles don't work at runtime in wpf, when using multiple MergedDictionaries?

开发者 https://www.devze.com 2023-02-12 08:24 出处:网络
If i use MergedDictionaries multiple times to define styles, it doesn\'t work at runtime, but in WPF Designer of VS2010 it works. It also work if load MergedDictionaries using code at runtime.

If i use MergedDictionaries multiple times to define styles, it doesn't work at runtime, but in WPF Designer of VS2010 it works. It also work if load MergedDictionaries using code at runtime.

Why this is happening ? Is it my problem only or ? And how to solve it ?

I am using WPF4 and loading themes/styles from an assembly at application level.

Not working

<!--Application.xaml-->
 <Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDic开发者_如何学Gotionaries>
            <ResourceDictionary Source="pack://application:,,,/Lib;component/Themes/Theme.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>


<!--Theme.xaml-->
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="Theme/Shared.xaml" />
    <ResourceDictionary Source="Theme/Button.xaml" />
</ResourceDictionary.MergedDictionaries>

Working

<Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="pack://application:,,,/Lib;component/Themes/Theme/Shared.xaml" />
                <ResourceDictionary Source="pack://application:,,,/Lib;component/Themes/Theme/Button.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>


See the answer to this question. I have a feeling you're having the same problem

This is an optimization bug, see this link

On the creation of every object in XAML, if a default style is present (i.e. style w/ a key of Type) that style should be applied. As you can imagine there are several performance optimizations to make that (implied) lookup a light weight as possible. One of them is that we don’t look inside Resource Dictionaries unless they are flagged as “containing default Styles”. There is a bug: if all your default styles are nested in merged dictionaries three levels deep (or deeper) the top dictionary does not get flagged so the search skips it. The work around is to put a default Style to something, anything, in the root Dictionary.

So adding a dummy style to the root dictionary fixes this. Example

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="pack://application:,,,/Lib;component/Themes/Theme.xaml" />
        </ResourceDictionary.MergedDictionaries>
        <!-- Dummy Style, anything you won't use goes -->
        <Style TargetType="{x:Type Rectangle}" />
    </ResourceDictionary>
</Application.Resources>
0

精彩评论

暂无评论...
验证码 换一张
取 消