I am using Visual Studio 2022 and .NET 6, WPF.
I have an application where I separated out all of my controls, styles, colors (all XAML resources) into a separate dll for sharing named Presentation.dll. I combined all the resources that I need into ResourceDictionary.MergedDictionaries xaml file named 'MyStyles.xaml', standard stuff:
<ResourceDictionary.MergedDictionaries>
<styles:SharedResourceDictionary Source="Presentation;component/Styles/ColorDefinitions.xaml"/>
<styles:SharedResourceDictionary Source="Presentation;component/Styles/FontDefinitions.xaml"/>
</ResourceDictionary.MergedDictionaries>
And then reference this resource dictionary inside Application.Resources.
<ResourceDictionary Source="/Presentation;component/Styles/MyStyles.xaml" />
The application csproj references Presentation csproj. Everything works fine, but I never needed to version Presentation.dll until now. The moment I attempt to set Version or AssemblyVersion, the application stops working and I get this exception:
System.Windows.Markup.XamlParseException
HResult=0x80131501
Message='Set property 'System.Windows.ResourceDictionary.DeferrableContent' threw an exception.' Line number '10' and line position '42'.
Source=PresentationFramework
StackTrace:
at System.Windows.Markup.XamlReader.RewrapException(Exception e, IXamlLineInfo lineInfo, Uri baseUri) in /_/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/XamlReader.cs:line 527
This exception was originally thrown at this call stack:
System.Windows.ResourceDictionary.SetDeferrableContent(System.Windows.DeferrableContent) in ResourceDictionary.cs
System.Windows.Baml2006.WpfSharedBamlSchemaContext.Create_BamlProperty_ResourceDictionary_DeferrableContent.AnonymousMethod__297_0(object, object) in WpfGeneratedKnownProperties.cs
System.Windows.Baml2006.WpfKnownMemberInvoker.SetValue(object, object) in WpfKnownMemberInvoker.cs
MS.Internal.Xaml.Runtime.ClrObjectRuntime.SetValue(System.Xaml.XamlMember, object, object) in ClrObjectRuntime.cs
MS.Internal.Xaml.Runtime.ClrObjectRuntime.SetValue(object, System.Xaml.XamlMember, object) in ClrObjectRuntime.cs
Inner Exception 1:
InvalidOperationException: Cannot re-initialize ResourceDictionary instance.
I am setting the version using Directory.Build.props file like this:
<Project>
<PropertyGroup>
<AssemblyVersion>1.0.0.0</AssemblyVersion>
<Version>1.0.0.0</Version>
</PropertyGroup>
</Project>
What am I missing here?
If I reference the compiled and versioned Presentation.dll in another solution and change source to <ResourceDictionary Source="pack://application:,,,/Presentation;component/Styles/MyStyles.xaml"/>, it loads fine. But if I reference the versioned project it gives the same error.
Additional notes: The application is actually HUGE and MyStyles.xaml contains several hundred definitions, controls, and templates. Because it has to run on x86 systems limited to 2GB RAM, I had to use SharedResourceDictionary to avoid running out of memory. Prior to trying to set the version on the Presentation project, everything worked fine.
Someone initially posted suggesting using version in the Source string but deleted their post. After replacing every single Source path in the codebase to include the version number (inside the versioned project and outside), it suddenly worked! I am not sure why. Maybe someone has a better suggestion.
<ResourceDictionary.MergedDictionaries>
<styles:SharedResourceDictionary Source="/Presentation;v1.0.0.0;component/Styles/ColorDefinitions.xaml"/>
<styles:SharedResourceDictionary Source="/Presentation;v1.0.0.0;component/Styles/FontDefinitions.xaml"/>
</ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Presentation;v1.0.0.0;component/Styles/MyStyles.xaml" />