Phantom719
Phantom719

Reputation: 41

Each dictionary entry must have an associated key error message

could someone please help me to solve this problem, I think I have coded right the key required. This is a Style application dictionary test with just one Button. There are two error messages: Each dictionary entry must have an associated key, and All objects added to an IDictionary must have a Key attribute or some other type of key associated with them. Line 13 Position 14, both for MainWIndow.xaml.

There are no more code written by programmer in this project.

This is the MainWindow.xaml code:

<Window x:Class="WpfApplication1.MainWindow"
    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"
    xmlns:local="clr-namespace:WpfApplication1"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525">
<Window.Resources>
    <ResourceDictionary>
        <ResourceDictionary Source="App.xaml" /> This is the offending line
    </ResourceDictionary>
</Window.Resources>
<Grid>
    <Button Style="{StaticResource algo}" />
</Grid>

And this is the App.xaml code:

<Application x:Class="WpfApplication1.App"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:local="clr-namespace:WpfApplication1"
         StartupUri="MainWindow.xaml">
<Application.Resources>
        <Style x:Key="algo" TargetType="{x:Type Button}">
            <Setter Property="Background" Value="Red" />
        </Style>
</Application.Resources>

Upvotes: 1

Views: 3688

Answers (2)

Phantom719
Phantom719

Reputation: 41

Thank you Funk, thank you Mathew Jibin, your explanations lead me to find what I think is the solution, it is as follows:

Modified App.xaml to be like this:

    <Application.Resources>
        <Style TargetType="Button">
            <Setter Property="Background" Value="Red" />
        </Style>
    </Application.Resources>

And modified MainWindow deleting everything in Grid:

    ...  
    Title="MainWindow" Height="350" Width="525">
    <Grid>
    </Grid>

Now, everytime I create a new button it has the desired Style. Please let me know if you see any possible problem with this solution. Thanks.

Upvotes: 2

Funk
Funk

Reputation: 11201

As pointed out in the comments, the syntax to reference a ResourceDictionary is

<Window.Resources>
  <ResourceDictionary>
    <ResourceDictionary.MergedDictionaries>
      <ResourceDictionary Source="myresourcedictionary.xaml"/>
    </ResourceDictionary.MergedDictionaries>
  </ResourceDictionary>
</Window.Resources>

Direct content of ResourceDictionary is considered to be the dictionary's entries. This is where the missing Key error refers to. A dictionary should have look up Keys for all its entries:

<Window.Resources>
    <ResourceDictionary>
        <conv:BoolToVisibilityConverter x:Key="BoolToVisibilityConverter" />
        <conv:BoolToCollapsedConverter  x:Key="BoolToCollapsedConverter" />
        ...
    </ResourceDictionary>
</Window.Resources>

An exception to the rule are implicit Styles (Styles that have a TargetType instead of a Key).

In your case none of the above will help, since Resources in App.xaml are treated special. They're considered Global Resources and can be referenced from everywhere. Trying to do reference them explicitly as in the first example will result in

An error occurred while finding the resource dictionary "App.xaml".

Instead change your MainWindow.xaml to

<Window x:Class="WpfApplication1.MainWindow"
    ...
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <Button Style="{StaticResource algo}" />
</Grid>

Upvotes: 2

Related Questions