资源字典无效 - 未找到名称/密钥的异常

时间:2017-06-23 05:16:17

标签: c# xaml resourcedictionary

我刚刚开始使用资源字典,我被困在这上面因为我的资源字典根本不起作用。我尝试过代码隐藏和XAML,但每次遇到异常并且应用程序崩溃。

如果我通过XAML引用Dictionary,我在运行时得到的异常是找不到Name / Key。我在App.xaml中使用的代码是:

<Application
x:Class="WatchfreeWebsite.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:WatchfreeWebsite.Helpers">

<Application.Resources>

    <TransitionCollection x:Key="TransCollection">
        <EdgeUIThemeTransition Edge="Right"/>
    </TransitionCollection>

    <ResourceDictionary x:Key="resourcesDictionary">
        <ResourceDictionary.MergedDictionaries>
            <local:GlobalTemplates Source="Helpers/GlobalTemplates.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>

</Application.Resources>

资源字典包含DataTemplateMediaTransportControlsStyle,但我似乎无法通过XAML引用它,因为它会产生语法错误,在运行时,页面在{{1}加载XAML时会产生异常阶段。

资源词典:

InitializeComponent();

资源字典背后的类是:

<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:WatchfreeWebsite.Helpers"
x:Class="WatchfreeWebsite.Helpers.GlobalTemplatesClass"
xmlns:data="using:WatchfreeWebsite.Helpers">


<DataTemplate x:Key="StreamBoxItemTemplate"
              x:DataType="data:StreamingHelper">
    <TextBlock Text="{x:Bind StreamName, Mode=OneWay}"
                       Style="{StaticResource BodyTextBlockStyle}"
                       TextWrapping="NoWrap"
                       MaxLines="1"
                       TextTrimming="WordEllipsis"/>
</DataTemplate>

<Style TargetType="MediaTransportControls"
       x:Key="myCustomTransportControls">
    <Setter Property="IsTabStop" Value="False" />
.......
</Style>

</ResourceDictionary>

我在上面的样式中引用了public partial class GlobalTemplatesClass { public GlobalTemplatesClass() { this.InitializeComponent(); } } ,这个样式在另一个页面中被引用为:

DataTemplate

但这不起作用,资源名称下方有一条红线表示找不到资源。

我有什么遗失的东西吗?如果有人可以帮助我,请提供您的建议。感谢

1 个答案:

答案 0 :(得分:0)

当您在资源下添加多个项目时,每个项目都应位于<ResourceDictionary>标记内,而不是直接位于<Application.Resources>下。

因为Resources 本身是一个字典,所以你实际上试图替换该集合而不是添加元素。文档:https://msdn.microsoft.com/en-us/library/system.windows.application.resources%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396

我在项目基础级别创建了一个只有App.xaml的示例项目,名为Helpers的文件夹和名为ResourceDictionary的{​​{1}}下的Helpers匹配你的。

Solution Explorer

GlobalTemplates.xaml

中创建了一个简单的画笔作为示例
GlobalTemplates.xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:App1.Helpers"> <SolidColorBrush x:Key="DefaultForeground" Color="DarkGreen" /> </ResourceDictionary>

App.xaml

然后在 <Application x:Class="App1.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:App1" RequestedTheme="Light"> <Application.Resources> <ResourceDictionary> <TransitionCollection x:Key="TransCollection"> <EdgeUIThemeTransition Edge="Right"/> </TransitionCollection> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="Helpers/GlobalTemplates.xaml"/> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> </Application.Resources> </Application> 成功引用了字典中的样式:

MainPage.xaml
相关问题