6

I am having a nagging issue with my App.Resources MergedDictionary. Every time add a new dictionary with a source and XML namespace from another assembly, an error is produced and I cannot build my program.

The error appears in App.xaml with the ResourceDictionary underlined and the message Value Cannot be Null. Parameter Name: item. This makes absolutely no sense as I have done the exact same thing before. The only difference is that this time, I am referencing the namespace of another assembly(c# class library). Here is the App.xaml file:

<Application x:Class="Quiz.Client.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:Quiz.Client"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Resources/Styles.xaml" />
                <ResourceDictionary Source="Resources/Templates.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

Below is Styles.xaml:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:local="clr-namespace:Quiz.Client">

    <Style x:Key="StrongFont"
           TargetType="ContentControl">
        <Setter Property="FontSize"
                Value="20" />
        <Setter Property="FontWeight"
                Value="ExtraBold" />
        <Setter Property="Foreground"
                Value="DarkRed" />
    </Style>


</ResourceDictionary>

Here is Templates.xaml:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:local="clr-namespace:Quiz.Client"
                    xmlns:domain="clr-namespace:Quiz.Core.Domain;assembly=Quiz.Core"
                    xmlns:common="clr-namespace:Quiz.Client.Common">

    <DataTemplate x:Key="ArithmeticQuestionTemplate"
                  DataType="domain:ArithmeticQuestion">

    </DataTemplate>

    <common:QuestionTemplateSelector x:Key="QuestionTemplateSelector"
                                     ArithmeticTemplate="{StaticResource ArithmeticQuestionTemplate}" />

</ResourceDictionary>

What the heck is going on? Was there a software-breaking change introduced or something? I am completely lost.

3 Answers3

1

Confirm!!! This bug is tracked by Microsoft. This mean WPF in some points will is trying to report to Microsoft automatically with Microsoft report process. During the report VS start freeze. Please, wait bug report to finish.

The bug still exist in lattes VS 2019.

After restart everything is fine. Confirm!!!

Thanks for the answer. I believe it is a Visual Studio bug caused by something unknown. Maybe one of my plugins, who knows. The only thing that fixes the issue is restarting visual studio and building the project. The problem seems to begin when adding a new namespace from another assembly. The project then can't build until visual studio is restarted. Very weird, never seen anything like it before. – user3096803

Pit
  • 395
  • 2
  • 11
0

Not sure why your method isn't working but it's not how I usually import resources myself. Typically I'll add a ResourceDictionary to my library project, put all the libraries resources in that and import it into my main project using pack syntax:

<Application.Resources>
    <ResourceDictionary>

        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="pack://application:,,,/MyLibrary;component/LibraryResources.xaml" />
        </ResourceDictionary.MergedDictionaries>

        <!-- main project resources go here -->

    </ResourceDictionary>

In general I'd argue that this is a better architecture anyway as it doesn't require a re-compile of your main project each time one of the child library resources change. It also makes it easier later on if you decide to switch to MEF, as your library resources are now encapsulated in a single ResourceDictionary object that you can readily [Export].

Mark Feldman
  • 15,731
  • 3
  • 31
  • 58
  • 1
    Thanks for the answer. I believe it is a Visual Studio bug caused by something unknown. Maybe one of my plugins, who knows. The only thing that fixes the issue is restarting visual studio and building the project. The problem seems to begin when adding a new namespace from another assembly. The project then can't build until visual studio is restarted. Very weird, never seen anything like it before. –  Feb 22 '16 at 23:52
0

I don't really understand the answer from @Pit but I tried restarting visual studios and also changing all source paths to full pack URIs, and nothing seemed to work. Came across this thread that hinted at using Tools > Options > Preview Features > New WPF XAML Designer for .NET Framework which fixed the issue for me.

EDIT: This "fix" may have been a red herring, it still had issues but they were equally cryptic! After switching back to the old XAML designer and doing some further digging I found that one of my merged dictionaries was incompatible with the wpf designer and it was resulting in this error. The fix on that page resolved my issue.

This specific fix probably won't help anyone, but hopefully the methodology will. Check if there are any errors in any of the individual resource dictionaries in the merged dictionaries.

chanban
  • 480
  • 1
  • 4
  • 19