c# – VS2012无法解析资源“X”

我的项目中有一个Resources.xaml文件,其中包含一个资源字典,如下所示:

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

    <Style x:Key="GPHeaderFontSize" TargetType="TextBlock">
        <Setter Property="FontWeight" Value="Bold" />
        <Setter Property="FontSize" Value="24" />
        <Setter Property="Text" Value="BLAHHHHH"/>
    </Style>
</ResourceDictionary>

我在App.xaml中包含了这个字典,如下所示:

<Application x:Class="GoldenPlains.App" 
    xmlns="schemas.microsoft.com/winfx/2006/xaml/presentation"; 
    xmlns:x="schemas.microsoft.com/winfx/2006/xaml"; 
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone" 
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone">
    <Application.Resources> 
        <local:LocalizedStrings xmlns:local="clr-namespace:GoldenPlains" x:Key="LocalizedStrings"/> 
        <ResourceDictionary x:Key="GPResources"> 
            <ResourceDictionary.MergedDictionaries> 
                <!-- Sometimes VS2012 complaining about path with blue line, please ignore it as path is correct --> 
                <ResourceDictionary Source="Styles/GPResources.xaml"/> 
            </ResourceDictionary.MergedDictionaries>
            <Style x:Key="GPRootOverlayBarStyle" TargetType="Image"> 
                <Setter Property="Source" Value="Assets/Images/root_brown_horizontal_bar.png"/> 
                <Setter Property="Width" Value="729"/> 
                <Setter Property="HorizontalAlignment" Value="Left"/> 
                <Setter Property="Stretch" Value="Uniform"/> 
            </style> 
    </Application.Resources>
    ...
    ...
</Application>

但是,当我尝试从另一个Page.xaml文件引用资源字典中的元素时,它似乎无法解析资源….
例如:
    

我试过使用像这样的绑定:

<TextBlock Style="{Binding Path=LocalizedResources.MyTextBlockStyle, Source=  {StaticResource GPResources}}"/>

它并不表示出现问题,但UI上没有显示任何内容.

正确方向上的一点很棒,欢呼声.

最佳答案 App.xaml中的资源字典定义应该类似于以下示例:

<Application.Resources>
    <ResourceDictionary>
        <local:LocalizedStrings xmlns:local="clr-namespace:GoldenPlains" x:Key="LocalizedStrings"/> 
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="Styles/GPResources.xaml"/> 
        </ResourceDictionary.MergedDictionaries>
        <!-- Other resources if you have -->
    </ResourceDictionary>
</Application.Resources>

然后,当您需要将Resources.xaml中定义的样式应用于UI控件时,只需引用样式的键/名称:

<TextBlock Style="{StaticResource GPHeaderFontSize}" />

注意:所有资源都需要在ResourceDictionary标记内,包括LocalizedStrings.

点赞