从Silverlight中的不同ResourceDictionary引用ResourceDictionary中的资源

Mik*_* S. 13 c# silverlight xaml resourcedictionary mergeddictionaries

我在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>
Run Code Online (Sandbox Code Playgroud)

NavigationPanel.xaml包含了风格,看起来像这样:

<Style x:Key="NavigationPanelListBox" TargetType="ListBox">
    <Setter Property="Background" Value="{StaticResource DarkBackground}" />
    <Lots of XAML>
</Style>
Run Code Online (Sandbox Code Playgroud)

{StaticResource的DarkBackground}在定义Brushes.xaml文件(即,第一资源字典).它被定义为

<SolidColorBrush x:Key="DarkBackground" Color="#FF707176" />
Run Code Online (Sandbox Code Playgroud)

在资源字典中.

在运行时,我收到以下错误:

Cannot find a Resource with the Name/Key DarkBackground [Line: 16 Position: 44]
Run Code Online (Sandbox Code Playgroud)

行号和位置引用app.xaml中的NavigationPanel.xaml资源字典.

我可以从其他控件引用画笔,而不是包含的资源字典.

为什么我不能引用或为什么它不解析对合并资源字典的层次结构更高的资源的引用?我在这里错过了什么?

Luk*_*der 13

您是否DarkBackgroundNavigationPanel字典中的任何资源中引用了画笔?

如果您是,您可能需要将Brushes资源字典合并到NavigationPanel字典中.

所以在NavigationPanel字典中.

<ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="/Client.Common;component/Theme/Brushes.xaml" />
</ResourceDictionary.MergedDictionaries>
Run Code Online (Sandbox Code Playgroud)


Adr*_*vel 8

你可以在另一个字典中包含一个字典(比如在C#中使用'),如下所示:

<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>
Run Code Online (Sandbox Code Playgroud)

这是你想要的?Cosmopolitan/Metro项目模板就是一个很好的例子......