I need to instantiate WPF types (say, a UserControl or a Page) via reflection for a designer. The problem I'm having is that when I attempt to instantiate these using Activator.CreateInstance I get a TargetInvocationException which wraps, in the end, an exception thrown by the StaticResource markup extension.
Clarification: The types are in a different assembly that is loaded at runtime!
Apparently, Activator.CreateInstance can't instantiate types that use the {StaticResource XXX} markup extension, even when the static resource is defined in the type's definition.
So, if all you have is the following type's definition (Type + xaml file):
<Page x:Class="Hurr.Durr">
<Page.Resources>
<ControlTemplate x:Key="whatever">
<TextBlock Text="This is a stupid example."/>
</ControlTemplate>
</Page.Resources>
<ContentControl Template="{StaticResource whatever}" />
</Page>
How would you spin this up at runtime?
BTW, requiring that you use DynamicResource instead is not acceptable. Does Visual Studio require this? Yet VS is able to spin up a copy of your WPF framework element and stick it in the designer.
Here's a sample application (beta 2, sorry) that demonstrates the issue.
http://cid-f8be9de57b85cc35.skydrive.live.com/self.aspx/Public/ResourcesGoKaboom.zip
The app attempts to load a Page with a drawing in its resource dictionary. I try to load it dynamically using Application.LoadComponent and Activator.CreateInstance and they both fail.
Okay, the example above works without an issue. The issue was that I was using a StaticResource within an element's attributes that referenced a resource defined within that element.
What was throwing me was that this works within visual studio. So in the designer everything looked okey dokey but IRL it was all pear shaped. IMHO, this is a bug and I've reported it as such:
https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=519244
Not sure if its a failure of VS to.... fail, or if the StaticResource extension was working in VS but not IRL.