【问题标题】:Cannot find a Resource with the Name/Key in windows phone 8 project在 windows phone 8 项目中找不到具有名称/密钥的资源
【发布时间】:2017-02-27 16:11:43
【问题描述】:

我创建了一个新的 Windows Phone 8 (Silverlight) 项目。 添加了Themes文件夹,在该文件夹中添加了两个资源字典,一个包含solidcolorbursh,一个包含colors

我在 App.xaml 中合并了这两个资源字典

在 MainPage.xaml 中,我使用了一个solidcolorbursh 作为网格背景。

代码片段:

App.xaml:

 <Application.Resources>
        <ResourceDictionary x:Key="mainStyle">
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="/resTest;component/Themes/MyColors.xaml"></ResourceDictionary>    
                <ResourceDictionary Source="/resTest;component/Themes/MyStyles.xaml"></ResourceDictionary>
            </ResourceDictionary.MergedDictionaries>
            <local:LocalizedStrings xmlns:local="clr-namespace:resTest" x:Key="LocalizedStrings"/>    
        </ResourceDictionary>           
    </Application.Resources>

MainPage.xaml:

<Grid x:Name="LayoutRoot" Background="{StaticResource RedSolidColorBrush}">
</Grid>

MyColors.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">

    <Color x:Key="RedColor">#FFFF0000</Color>
</ResourceDictionary>

MyStyles.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="RedSolidColorBrush" Color="{StaticResource RedColor}"></SolidColorBrush>
</ResourceDictionary>

抛出异常:

块引用

System.Windows.ni.dll 中的“System.Windows.Markup.XamlParseException” 它显示运行时错误:

抛出异常:“System.Windows.Markup.XamlParseException”在 System.Windows.ni.dll

附加信息:找不到具有名称/键的资源 RedColor [行:14 位置:44]

我哪里错了?

参考文献https://msdn.microsoft.com/en-us/library/cc903952(VS.95).aspx

https://joshsmithonwpf.wordpress.com/2010/09/24/consuming-resources-from-external-assemblies-in-silverlight-4/

Referencing a merged resource dictionary in windows phone seven failing

【问题讨论】:

    标签: c# xaml silverlight windows-phone-8 windows-phone


    【解决方案1】:

    您需要能够从MyStyles.xaml 中看到MyColors.xaml,否则将无法找到RedColor 的定义。

    如果您在MyStyles.xaml 中包含以下内容:

    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="...";component/MyColors.xaml" />
    </ResourceDictionary.MergedDictionaries>
    

    其中“...”是MyColors.xaml的路径,那么你应该可以参考其中定义的颜色。

    尽管像您已经完成的那样合并App.xaml 中的字典应该会产生相同的效果。如果您确实将MyStyles.xaml 更改为包含MyColors.xaml,则应从App.xaml 中删除对MyColors.xaml 的引用。

    【讨论】:

      【解决方案2】:

      首先: 如果 ResourceDictionary 文件的构建操作设置为 Resource,您可以通过以下方式引用它:

      /<assembly name>;component/<resource file path>
      

      如果构建操作设置为内容,那么您可以通过这种方式引用它:

      /<resource file path>
      

      然后: 我们需要在 MyStyles 中合并 MyColors。 谢谢@ChrisF

      其实我习惯用 Windows Phone 8.1 或 UWP 编码,没有这些问题。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-01-05
        • 2012-10-31
        • 1970-01-01
        • 2017-02-26
        • 1970-01-01
        • 2022-01-24
        • 1970-01-01
        • 2012-10-13
        相关资源
        最近更新 更多