0

I have a WPF Window that need to be Unit Tested, so I need to create an instance of it using C# code. The Window is using some DataTemplate available in a separate dictionary. If I run the app everything is fine, but when I create a new instance of the Window in my unit tests, using this code:

MainWindow mockWindow = new MainWindow();

It throws the following exception:

Test method 
[xxx] threw exception: 
System.Windows.Markup.XamlParseException: 'Provide value on 'System.Windows.StaticResourceExtension' threw an exception.' 
Line number '48' and line position '30'. ---> 
System.Exception: Cannot find resource named 'TreeViewItemTemplate'. 
Resource names are case sensitive.

I assume that "somehow" I need to initialize the resources before creating the view. How can I do that programmatically?

3
  • 1
    have you tried using DynamicResource? Commented Jan 17, 2013 at 9:53
  • You mean, instead of using StaticResource? I will give it a try now Commented Jan 17, 2013 at 9:55
  • It works, I have to use DynamicResource in order to get them resolved also programmatically! Thanks Commented Jan 17, 2013 at 9:56

1 Answer 1

1

use DynamicResource instead of StaticResource.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.