Creating a System.Windows.Controls.Image throws an exception - how do I use the dispatcher to instan

Posted by Scott Whitlock on Stack Overflow See other posts from Stack Overflow or by Scott Whitlock
Published on 2010-06-09T03:32:29Z Indexed on 2010/06/09 3:42 UTC
Read the original article Hit count: 163

Filed under:
|
|

I'm running my unit tests on a piece of code that does the following in the test:

Assert.IsNotNull(target.Icon);

Inside the getter for the Icon property, I'm doing this:

System.Windows.Controls.Image img = new System.Windows.Controls.Image();

That's throwing this exception: System.InvalidOperationException : The calling thread must be STA, because many UI components require this.

I understand what that means, and I understand that I need to use the Dispatcher, but I'm a bit confused about how or why... this is a property of my ViewModel and I don't get any of these exceptions when running the application.

Other info: this only started failing when I upgraded to .NET 4.

© Stack Overflow or respective owner

Related posts about c#

Related posts about unit-testing