Adding a Merged Dictionary to a Merged Dictionary

后端 未结 3 1614
灰色年华
灰色年华 2020-12-03 01:19

I can\'t seem to be able to add a merged dictionary to a collection of merged dictionaries within XAML.

Theme.xaml



        
相关标签:
3条回答
  • 2020-12-03 02:04

    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="/Mine;component/Themes/Theme.xaml"/>
            </ResourceDictionary.MergedDictionaries>
            <!-- Dummy Style, anything you won't use goes -->
            <Style TargetType="{x:Type Rectangle}" />
        </ResourceDictionary>
    </Application.Resources>
    
    0 讨论(0)
  • 2020-12-03 02:15

    Your sample code has a double equal sign in the App.xaml merged resource dictionary source for Palette.Blue.xaml. I'm assuming this is a typo for your example posted here, and not your real problem though.

    It can be tricky to figure out how to link all the resources directly in XAML. The easiest way to do it is from the Resources panel in Blend. I created a Silverlight app with resource files named like your example, then opened the project in Blend and very quickly linked them together.

    App.xaml

    <Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Class="SilverlightApplication1.App">
        <Application.Resources>
            <ResourceDictionary>
                <ResourceDictionary.MergedDictionaries>
                    <ResourceDictionary Source="Theme.xaml" />
                    <!--
                    <ResourceDictionary Source="Palette.Blue.xaml"/>
                    <ResourceDictionary Source="Template.xaml"/>
                    -->
                </ResourceDictionary.MergedDictionaries>
            </ResourceDictionary>
        </Application.Resources>
    </Application>
    

    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="Palette.Blue.xaml"/>
            <ResourceDictionary Source="Template.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
    

    Template.xaml

    <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
        <Style TargetType="TextBox">
            <Setter Property="Margin" Value="10" />
            <Setter Property="Width" Value="250" />
        </Style>
        <Style x:Key="ReadOnlyTextBoxStyle" TargetType="TextBox">
            <Setter Property="IsReadOnly" Value="True" />
            <Setter Property="Foreground" Value="Black" />
            <Setter Property="IsTabStop" Value="False" />
            <Setter Property="Margin" Value="10" />
            <Setter Property="Width" Value="250" />
        </Style>
    </ResourceDictionary>
    

    Palette.Blue.xaml

    <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
        <SolidColorBrush x:Key="BlueSolidColorBrush" Color="SkyBlue" />
    </ResourceDictionary>
    

    MainPage.xaml

    <UserControl x:Class="SilverlightApplication1.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
        <StackPanel x:Name="LayoutRoot" Background="Honeydew">
            <TextBox Text="Read Only Textbox"
                     Style="{StaticResource ReadOnlyTextBoxStyle}" />
            <TextBox Text="Blue Textbox"
                     Background="{StaticResource BlueSolidColorBrush}" />
            <TextBox Text="Read Only, Blue Textbox"
                     Style="{StaticResource ReadOnlyTextBoxStyle}"
                     Background="{StaticResource BlueSolidColorBrush}" />
        </StackPanel>
    </UserControl>
    

    Of course, if you're linking resources from different assemblies it will look different. Actually, in that case, I'd suggest looking into merging your dictionaries in the code behind.

    0 讨论(0)
  • 2020-12-03 02:16

    If this is happening on one of your own controls I've found that another solution is setting the DefaultStyleKey property to null:

    DefaultStyleKeyProperty.OverrideMetadata(typeof(MyControl), new FrameworkPropertyMetadata(null));
    

    I have no idea why this works, but it seems to!

    0 讨论(0)
提交回复
热议问题