Search code examples
c#xamlresourcedictionary

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


I have just started working with Resource Dictionaries and I am stuck on this because my resource dictionary is not working at all. I have tried code-behind and XAML but every time I get exceptions and the app crashes.

If I reference the Dictionary through XAML I get the exception at runtime that Name/Key is not found. The code I used in App.xaml is:

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

The resource dictionary holds aDataTemplate and a MediaTransportControlsStyle but I cant seem to reference it through XAML because it gives syntax errors and during the runtime the page produces exception while loading XAML at InitializeComponent(); stage.

Resource Dictionary:

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

The class behind the resource dictionary is:

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

I reference the DataTemplate inside the above style and this style is referenced in another page as:

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

But this is not working and there is a red line below the resource name saying that the resource is not found.

Is there something that I am missing? If someone can help me here please provide your suggestions. Thanks


Solution

  • When you add multiple items under your resources, each of them should fall within the <ResourceDictionary> tag and not directly under <Application.Resources>.

    That's because Resources itself is a dictionary, so you're in effect trying to replace that collection rather than add elements to it. Docs here: https://msdn.microsoft.com/en-us/library/system.windows.application.resources%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396

    I created a sample project with just an App.xaml at the project base level, a folder called Helpers and a ResourceDictionary under Helpers named GlobalTemplates.xaml to match yours.

    Solution Explorer

    Created a simple brush as an example in 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> 
    

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

    And then in MainPage.xaml successfully referenced the style from the dictionary:

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