找不到具有Name / Key NavigationPaneButton的资源

时间:2015-11-16 07:17:17

标签: c# windows xaml uwp resourcedictionary

我正在尝试在styles.xaml中引用NavigationPaneButton资源。我已经将它们全部定义在不同的资源文件中,并使用正确的顺序在app.xaml中正确地链接它们。但我仍然得到例外

  

“找不到名称/密钥NavigationPaneButton的资源”

我有什么遗失的吗?

<ResourceDictionary
   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
   xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
   xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
   mc:Ignorable="d">

    <Style TargetType="Button" x:Key="WasHamburguerButton">
        <Setter Property="Background" Value="{ThemeResource NavigationPaneButton}" />
        <Setter Property="Foreground" Value="{ThemeResource NavigationPaneText}"/>
    </Style>

</ResourceDictionary>

我在AppTheme.xaml中定义了资源

<ResourceDictionary
   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
   xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
   xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
   mc:Ignorable="d">
    <SolidColorBrush x:Key="NavigationPaneText" Color="{ThemeResource NavigationPaneTextColor}"/>
    <SolidColorBrush x:Key="NavigationPaneButton" Color="{ThemeResource NavigationPaneButtonColor}"/>

</ResourceDictionary>

AppColor.xaml中的颜色

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <ResourceDictionary.ThemeDictionaries>
        <ResourceDictionary x:Key="Default">
            <Color x:Key="NavigationPaneTextColor">#ffffff</Color>
            <Color x:Key="NavigationPaneButtonColor">#D13438</Color>
        </ResourceDictionary>
    </ResourceDictionary.ThemeDictionaries>
</ResourceDictionary>

这是我的app.xaml

<Application
    x:Class="WindowsUniversalApp.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:WindowsUniversalApp">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Resources/AppColors.xaml"/>
                <ResourceDictionary Source="Resources/AppTheme.xaml"/>
                <ResourceDictionary Source="Resources/Styles.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>

    </Application.Resources>

</Application>

1 个答案:

答案 0 :(得分:2)

如果你像这样链接它们将会起作用:

样式 - &gt; AppTheme - &gt; AppColors

所以,AppTheme了解AppColors:

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:TestImage.Resources">
    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="ms-appx:///Resources/AppColors.xaml"/>
    </ResourceDictionary.MergedDictionaries>
    <SolidColorBrush x:Key="NavigationPaneText" Color="{ThemeResource NavigationPaneTextColor}"/>
    <SolidColorBrush x:Key="NavigationPaneButton" Color="{ThemeResource NavigationPaneButtonColor}"/>
</ResourceDictionary>

Styles了解AppTheme:

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:TestImage.Resources">
    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="ms-appx:///Resources/AppTheme.xaml"/>
    </ResourceDictionary.MergedDictionaries>
    <Style TargetType="Button" x:Key="WasHamburguerButton">
        <Setter Property="Background" Value="{ThemeResource NavigationPaneButton}" />
        <Setter Property="Foreground" Value="{ThemeResource NavigationPaneText}"/>
    </Style>
</ResourceDictionary>