15

App.xamlに次のコードセットがあります。

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="/Client.Common;component/Theme/Brushes.xaml"/>
            <ResourceDictionary Source="/Client.Common;component/Theme/Fonts.xaml"/>
            <ResourceDictionary Source="/Client.Common;component/Theme/CoreStyles.xaml"/>
            <ResourceDictionary Source="/Client.Common;component/Theme/SdkStyles.xaml"/>
            <ResourceDictionary Source="/Client.Common;component/Theme/MyAppName.xaml"/>

            <ResourceDictionary Source="/Client.Common;component/Controls/NavigationPanel.xaml"/>
         </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

NavigationPanel.xamlには、次のようなスタイルが含まれています。

<Style x:Key="NavigationPanelListBox" TargetType="ListBox">
    <Setter Property="Background" Value="{StaticResource DarkBackground}" />
    <Lots of XAML>
</Style>

{StaticResource DarkBackground}は、Brushes.xamlファイル(つまり、最初のリソースディクショナリ)で定義されています。それは次のように定義されます

<SolidColorBrush x:Key="DarkBackground" Color="#FF707176" />

リソース辞書にあります。

実行時に、次のエラーが発生します。

Cannot find a Resource with the Name/Key DarkBackground [Line: 16 Position: 44]

行番号と位置は、app.xamlのNavigationPanel.xamlリソースディクショナリを参照します。

含まれているリソースディクショナリだけでなく、他のコントロールからブラシを参照できます。

なぜ参照できないのか、またはマージされたリソースディクショナリの階層の上位にあるリソースへの参照を解決しないのですか?ここで何が欠けていますか?

4

3 に答える 3

14

辞書DarkBackgroundのリソースのいずれかでブラシを参照していますか?NavigationPanel

もしそうなら、Brushesリソースディクショナリをディクショナリにマージする必要があるかもしれませんNavigationPanel

したがって、NavigationPanelディクショナリで。

<ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="/Client.Common;component/Theme/Brushes.xaml" />
</ResourceDictionary.MergedDictionaries>
于 2012-02-29T07:39:06.033 に答える
8

次のように、ある辞書を別の辞書に含めることができます(C#の「using」など)。

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation" 
    xmlns:uriMapper="clr-namespace:System.Windows.Navigation;assembly=System.Windows.Controls.Navigation"
    xmlns:sys="clr-namespace:System;assembly=mscorlib"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:Controls="clr-namespace:APC.IKM.UI.SL.Controls"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d">

    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="Brushes.xaml"/>
        <ResourceDictionary Source="Fonts.xaml"/>
        <ResourceDictionary Source="CoreStyles.xaml"/>
    </ResourceDictionary.MergedDictionaries>

これはあなたが探しているものですか?コスモポリタン/メトロプロジェクトテンプレートには、この良い例があります...

于 2012-03-01T05:23:48.163 に答える
0

本当に答えは、このサイトでのエリックの答えです:https ://social.msdn.microsoft.com/forums/windowsapps/en-US/2be9a5f6-5313-448d-a9d9-296bac42215e/using-style-defined-in-merged- dictionary-from-another-merged-dictionary?forum=wpdevelop
Brushes.xamlとNavigationPanel.xamlは個別に解析されてから、アプリケーションリソースのマージされたディクショナリに追加されるため、お互いについて何も知りません。

于 2016-06-08T09:07:33.377 に答える