locked
WPF -> Activator.CreateInstance -> Resource not found RRS feed

  • Question

  • Hello,

    I'm working on the WPF Designer component in Sharp Develop! There we use Activator.CreateInstance to create a Instance of a Control. This now does not work, because the Control does not find some of it's resources! I know I could fix this if I put the Resources in my app.xaml ResourceDictionary, but this is not possible, because the Designer then also uses the Resources for it's elements! Is there a another way to create a Instance of a control?

    Tuesday, December 1, 2015 7:46 AM

Answers

  • Several.

    I don't really follow how they're going to help load resources for something that's dependent on them though.

    At least from reading your question...

    I would think your problem isn't how to new up the control, it's how to do so without it wanting it's dependencies.

    You could put designer resources in the control I suppose.

    That doesn't sound like a great plan but that's a way to give them scope to the control.

    You can use the approach described here:

    http://www.wpfsharp.com/2012/02/02/how-to-load-a-resourcedictionary-at-design-time-in-wpf/

    Or you could go with code and load the resources in the constructor before initializecomponent builds the controls from xaml.

    public MainWindow()
    {
        if (DesignerProperties.GetIsInDesignMode(new DependencyObject())) 
        {
            // Get your dependencies from wherever and add to this.Resources
        }
        InitializeComponent();


    Hope that helps.

    Technet articles: WPF: MVVM Step 1; All my Technet Articles

    Tuesday, December 1, 2015 9:24 AM
  • >>Is there a another way to create a Instance of a control?

    Not besides creating an instance of a compile-time known type using the new operator as usual:

    YourControl theControl = new YourControl();

    So whatever you are trying to do you need to reconsider your approach. Don't use external resources at all or make sure that all necassary resources are found as expected. These are pretty much your options.

    Please upload a reproducible sample of your issue to OneDrive and post the link to it here along with a detailed description about how to reproduce it if you need any further help.

    Please also remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.

    Tuesday, December 1, 2015 4:42 PM

All replies

  • Several.

    I don't really follow how they're going to help load resources for something that's dependent on them though.

    At least from reading your question...

    I would think your problem isn't how to new up the control, it's how to do so without it wanting it's dependencies.

    You could put designer resources in the control I suppose.

    That doesn't sound like a great plan but that's a way to give them scope to the control.

    You can use the approach described here:

    http://www.wpfsharp.com/2012/02/02/how-to-load-a-resourcedictionary-at-design-time-in-wpf/

    Or you could go with code and load the resources in the constructor before initializecomponent builds the controls from xaml.

    public MainWindow()
    {
        if (DesignerProperties.GetIsInDesignMode(new DependencyObject())) 
        {
            // Get your dependencies from wherever and add to this.Resources
        }
        InitializeComponent();


    Hope that helps.

    Technet articles: WPF: MVVM Step 1; All my Technet Articles

    Tuesday, December 1, 2015 9:24 AM
  • I've loaded the ResourceDictionary with the needed Resources already, but I can not modify the Control which does not work, because it's a WPF Designer and I cannot modify the used Controls! And in normal Project you use this Controls you put the Dictionarys in the app.xaml!
    Tuesday, December 1, 2015 2:19 PM
  • >>Is there a another way to create a Instance of a control?

    Not besides creating an instance of a compile-time known type using the new operator as usual:

    YourControl theControl = new YourControl();

    So whatever you are trying to do you need to reconsider your approach. Don't use external resources at all or make sure that all necassary resources are found as expected. These are pretty much your options.

    Please upload a reproducible sample of your issue to OneDrive and post the link to it here along with a detailed description about how to reproduce it if you need any further help.

    Please also remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.

    Tuesday, December 1, 2015 4:42 PM
  • Problem is, my Application is a WPF Designer so I need a way to create a Instance without adding the Resources to my App.xaml. I've added a Demo, this throws a Exception, until you enable the commented Code in App.xaml!

    The Problem comes from the StaticResourceExtension not finding the resource...

    I have my own IServiceProvider implementation, wich give my own IAmbientProvider, but I have no way to create a Instance using my own implementations! (You see here http://referencesource.microsoft.com/#PresentationFramework/src/Framework/System/Windows/StaticResourceExtension.cs

    in Line 147 that IAmbientProvider is used). But how could I create a Instance of a Control that it internal uses my own IServiceProvider

    https://dl.dropboxusercontent.com/u/101855172/ResourceError.zip

    The Code in "TestLibrary" you can see as closed Source, a object wich I could not modify!


    Tuesday, December 1, 2015 6:43 PM
  • I'm still confused by what you're doing here really.

    If you have no app.xaml you can start up an Application() anyhow.

    Then add a resource to application.current.resources.
    And you can do that at design time if you have the relevant source code for the thing.

    If you don't then you can use design time resources in the project your control is in.

    If you can't do either then what are you editing that you don't have the source for either the control designer or the project you're using it in?


    Hope that helps.

    Technet articles: WPF: Layout Lab; All my Technet Articles

    Tuesday, December 1, 2015 7:50 PM
  • i have a app.xaml, but i'm creating a designer application, so if i load external resource dictionarys to my app.xaml, my whole app will be restyled (maybe)! so i load resources only to my hosting element, but then they are not found when using Activator.CreateInstance
    Wednesday, December 2, 2015 5:45 AM
  • Noone any Idea? If simeone is interested, the Project I work on is here: https://github.com/icsharpcode/WpfDesigner
    Friday, December 4, 2015 5:22 PM
  • Can anyone help here?

    I think relatively few devs make controls with designers and even less will be working with ones that have external dependencies.


    Hope that helps.

    Technet articles: WPF: Layout Lab; All my Technet Articles

    Monday, December 7, 2015 8:49 PM
  • How should that Help? *confused*
    Tuesday, December 8, 2015 5:30 AM
  • >I think relatively few devs make controls with designers and even less will be working with ones that have external dependencies.

    That's an explanation of why I think you don't have an answer.

    Very few people do this stuff.

    The people who mostly answer threads here have read your question and in my case had a go at trying to help.

    Nobody who has read the thread has a helpful answer for you.

    So far.

    That could change.

    I don't want to be pessimistic but it's not looking good.

    You need some luck.


    Hope that helps.

    Technet articles: WPF: Layout Lab; All my Technet Articles

    Tuesday, December 8, 2015 11:33 AM