0

Everything happens within the same VS project. I have a resource dictionary file living on it's own. When I try to load it programmatically I get the error

"Cannot create unknown type '{clr-namespace:MyAssembly.Helpers}IsNullConverter".

Here is how I load it :

StreamResourceInfo stream = Application.GetResourceStream(new Uri(@"MyAssembly;component/Resources/Resources.xaml", UriKind.Relative));

this.dynamicResources = XamlReader.Load(stream.Stream) as ResourceDictionary;

And here is the resource dictionary :

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
                    xmlns:helpers="clr-namespace:MyAssembly.Helpers">

<helpers:IsNullConverter x:Key="IsNullConverter" />

Styles go here...

Note that it is tied to a code-behind file, but there is nothing in it. The Build-Action of the resource file is set to "Resource". This is driving me crazy since this morning and still no clue what the heck is going on...

Help. Thank you.

Mohamed
  • 121
  • 1
  • 7
  • Where is the `MyAssembly.Helpers.IsNulLConverter` defined? The parser is looking for the class under that supplied path and failing to find the expected metadata... – code4life Sep 24 '12 at 17:40

2 Answers2

1

Halelujah I fugured it out. All I had to do is load the resource dictionary directly

Uri uri = new Uri(@MyAssembly;component/Resources/Resources.xaml", UriKind.Relative); this.dynamicResources.Source = uri;

And make sure Build Action of resource dictionary file is set to "Page"

\m/

Mohamed
  • 121
  • 1
  • 7
0

Is the assembly referenced by your project? If not try adding a reference - if you don't want a dependency you could try loading the assembly:

http://www.dreamincode.net/forums/topic/78974-using-reflection-to-load-unreferenced-assemblies-at-runtime/

Alternatively you could add a x:Class definition to the resourcedictionary, and instantiate the class from the assembly instead of loading the xaml, and remember to call the generated InitializeComponent() from the constructor, the it will load.

Is it possible to set code behind a resource dictionary in WPF for event handling?

Your example would work fine if the ResourceDictionary and converter was in the same assembly as where you load from, as far as I can see :)

Community
  • 1
  • 1
Rune Andersen
  • 1,650
  • 12
  • 15
  • Thanks for answering, but that did not solve the problem. I figured it out, I'll post the answer. – Mohamed Sep 25 '12 at 14:19