21

XAML 内のマージされた辞書のコレクションにマージされた辞書を追加できないようです。

テーマ.xaml

<ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="/Mine;component/Themes/Palette.Blue.xaml"/>
    <ResourceDictionary Source="/Mine;component/Themes/Template.xaml"/>
</ResourceDictionary.MergedDictionaries>

アプリケーション リソース

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="/Mine;component/Themes/Theme.xaml"/> 
            <!--
            <ResourceDictionary Source=="/Mine;component/Themes/Palette.Blue.xaml"/>
            <ResourceDictionary Source="/Mine;component/Themes/Template.xaml"/>
            -->
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

注: 両方の ResourceDictionaries を Appication.Resources MergedDictionary に配置すると (theme.xaml をコメント アウトし、他の 2 つの辞書のコメントを外します)、両方とも正しく読み込まれます。ただし、リソースの定義方法によっては、かなりの数のリソースが読み込まれる可能性があり、動的読み込みのためにテンプレートを定義できるようにしたいと考えています。

4

3 に答える 3

35

これは最適化のバグです。このリンクを参照してください

XAML でのすべてのオブジェクトの作成時に、既定のスタイル (つまり、Type のキーを持つスタイル) が存在する場合、そのスタイルを適用する必要があります。ご想像のとおり、その (暗黙の) ルックアップを可能な限り軽量にするために、いくつかのパフォーマンスの最適化が行われています。その 1 つは、リソース ディクショナリが「既定のスタイルを含む」というフラグが立てられていない限り、リソース ディクショナリ内を調べないことです。バグがあります: すべてのデフォルト スタイルがマージされた辞書に 3 レベル (またはそれ以上) ネストされている場合、最上位の辞書にフラグが立てられないため、検索がスキップされます。回避策は、ルート ディクショナリ内の何かにデフォルトのスタイルを設定することです。

したがって、ルート ディクショナリにダミー スタイルを追加すると、これが修正されます。例

<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>
于 2010-11-06T15:00:07.383 に答える
1

サンプルコードには、Palette.Blue.xamlのApp.xamlマージ済みリソースディクショナリソースに二重等号があります。これは、ここに投稿された例のタイプミスであり、実際の問題ではないと思います。

XAMLですべてのリソースを直接リンクする方法を理解するのは難しい場合があります。これを行う最も簡単な方法は、Blendの[リソース]パネルからです。例のような名前のリソースファイルを使用してSilverlightアプリを作成し、Blendでプロジェクトを開いて、それらをすばやくリンクしました。

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>

もちろん、異なるアセンブリのリソースをリンクしている場合は、外観が異なります。実際、その場合は、辞書をコードビハインドにマージすることを検討することをお勧めします。

于 2010-08-12T13:12:07.527 に答える
0

これが独自のコントロールの 1 つで発生している場合、別の解決策がDefaultStyleKeyプロパティを null に設定していることがわかりました。

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

なぜこれが機能するのかわかりませんが、そうです!

于 2016-09-23T17:10:22.643 に答える