【问题标题】:Resource Dictionary not working - Exception raised for Name/Key not found资源字典不起作用 - 未找到名称/键引发异常
【发布时间】:2017-06-23 05:16:17
【问题描述】:

我刚刚开始使用资源字典,但由于我的资源字典根本无法正常工作,所以我一直坚持这一点。我尝试过代码隐藏和 XAML,但每次遇到异常并且应用程序崩溃时。

如果我通过 XAML 引用字典,我会在运行时收到未找到名称/密钥的异常。我在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 引用它,因为它会给出语法错误,并且在运行时页面在InitializeComponent(); 阶段加载XAML 时会产生异常。

资源字典:

<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,这个样式在另一个页面中被引用为:

 <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>

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

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

【问题讨论】:

    标签: c# xaml resourcedictionary


    【解决方案1】:

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

    那是因为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 的 ResourceDictionary 名为 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成功引用了字典中的样式:

    <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>
    

    【讨论】:

    • 我的应用实际上是一个 UWP 应用,它说我需要字典的 x:key 属性。即使我把它放在那里它仍然不起作用。
    • 我已经更新了我的答案。您的附加资源应位于 标记内,而不是在其之外。
    • 是的,我现在明白了。显然我的项目存在某种问题,我创建了一个新的示例项目,并且能够完全按照您的方式进行操作。所以我将我的应用程序移植到新项目中,现在它可以工作了。我认为由于这个问题,它确实让我向Application.Resources 标签添加了多个资源,但在新项目中它不允许我这样做,我必须将它们放在ResourceDictionary 中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-11
    • 1970-01-01
    • 2013-10-01
    相关资源
    最近更新 更多