Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 498097
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T05:50:41+00:00 2026-05-13T05:50:41+00:00

I need to instantiate WPF types (say, a UserControl or a Page) via reflection

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T05:50:41+00:00Added an answer on May 13, 2026 at 5:50 am

    The problem appears to be with the Page you’re trying to load. Your Page looks like this (omitting a bunch of xmlns etc. plumbing):

    <AwesomePage Title="AwesomePage" Background="{StaticResource awesomeface}">
        <AwesomePage.Resources>
            <DrawingBrush x:Key="awesomeface" Stretch="Uniform">
                <!-- details snipped -->
            </DrawingBrush>
        </AwesomePage.Resources>
    </AwesomePage>
    

    This tries to use the awesomeface resource before it is defined. StaticResource does not allow this kind of forward referencing: therefore the StaticResourceExtension.ProvideValue fails and you get the error you have run into.

    To fix this, put a Border immediately inside the Page, and move the Background setter from the Page to the Border. Then put all your content inside the Border.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem with reflection. I need to find the type that instantiates
Aloha, I have a custom control that I need to instantiate from within a
Need a function that takes a character as a parameter and returns true if
Need to an expression that returns only things with an I followed by either
Need a function like: function isGoogleURL(url) { ... } that returns true iff URL
Background: I'm using WPF and C# (3.5) and am working on an app that
Ok, I have a ResourceDictionary definition that I used for a WPF app below:
Is a separate WPF value converter object instantiated for each binding that a particular
I need to instantiate a Winform within another project. How is this done? I
I have a function that uses reflection to set properties of object A from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.