资源字典不工作 - 未找到 Name/Key 引发的异常

Resource Dictionary not working - Exception raised for Name/Key not found

我刚开始使用资源字典,但一直坚持下去,因为我的资源字典根本不起作用。我已经尝试过代码隐藏和 XAML 但每次我遇到异常并且应用程序崩溃时。

如果我通过 XAML 引用词典,我会在运行时遇到异常,即找不到 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>

资源字典包含一个 DataTemplate 和一个 MediaTransportControlsStyle 但我似乎无法通过 XAML 引用它,因为它会给出语法错误并且在运行时页面在加载时会产生异常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>

资源字典后面的class是:

public partial class GlobalTemplatesClass
{
    public GlobalTemplatesClass()
    {
        this.InitializeComponent();
    }
}

我在上面的样式中引用了 DataTemplate 并且这个样式在另一个页面中被引用为:

 <MediaPlayerElement x:Name="MediaView"
                            Grid.Row="2"
                            Source="{Binding MediaSourceObject, Mode=OneWay}"
                            DoubleTapped="MediaView_DoubleTapped"
                            AreTransportControlsEnabled="True">
            <MediaPlayerElement.TransportControls>
                <data:CustomTransportControlsHelper Style="{StaticResource ResourceKey=myCustomTransportControls}"/>
            </MediaPlayerElement.TransportControls>
        </MediaPlayerElement>

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

有什么我想念的吗?如果有人可以在这里帮助我,请提供您的建议。谢谢

当您在资源下添加多个项目时,每个项目都应位于 <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 的文件夹和 Helpers 下名为 [=20= 的 ResourceDictionary ] 来匹配你的。

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成功从字典中引用了样式:

<Page
    x:Class="App1.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:App1"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">
    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <TextBlock Foreground="{StaticResource DefaultForeground}">Hello world</TextBlock>
    </Grid>
</Page>