Question

In trying to set a default ResourceDictionary I receive the following warning:

The designer does not support loading dictionaries that mix 'ResourceDictionary' items without a key and other items in the same collection. Please ensure that the 'Resources' property does not contain 'ResourceDictionary' items without a key, or that the 'ResourceDictionary' item is the only element in the collection.

This is the code that I am using in my App.xaml file, that received the above warning:

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="Lang.en-US.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

This is the exact same code that I've used to set a ResourceDictionary in Visual Studio 2008. I am now using VS 2010. What Key do I need to provide this ResourceDictionary for it to work correctly?

This is the line in my MainWindow.xaml that I am currently testing along with this code:

<MenuItem Header="{DynamicResource new_test}" />
Was it helpful?

Solution

Since you haven't posted your complete XAML file, i suspect there are other resources apart from merged dictionary in your resources section.

As per MSDN -

It is legal to define resources within a ResourceDictionary that is specified as a merged dictionary, either as an alternative to specifying Source, or in addition to whatever resources are included from the specified source. However, this is not a common scenario; the main scenario for merged dictionaries is to merge resources from external file locations. If you want to specify resources within the markup for a page, you should typically define these in the main ResourceDictionary and not in the merged dictionaries.

Try moving other resources in separate resource dictionary and make sure all other resources have x:Key set on them -

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="Lang.en-US.xaml" />
        </ResourceDictionary.MergedDictionaries>
        <ResourceDictionary>
             <BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/>
             <ContextMenu x:Key="MyContextMenu"/>
        </ResourceDictionary>
    </ResourceDictionary>
</Application.Resources>

OTHER TIPS

Use resource file for translations. Its better than resource dictionary.

Here is an example:

Set prefix like this for usage in xaml.

xmlns:const="clr-namespace:FileExplorer.Properties"

Resources are located in properties.

To use them in XAML you will need following:

<TextBox Text="{x:Static const:Resources.Window_Title_String}"/>

If you have different languages then create for each language own resource file following naming convention.

For example:

Resources.resx (this will be default)
Resources.de-DE.resx (this is for german)

Now you just have to set current culture to german for your app to be on german and the proper resource file will be used automatically.

Like this in Main method:

Thread.CurrentThread.CurrentUICulture = new CultureInfo("de-DE");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top