How do I add a StaticResource at design time? RRS feed

  • Question

  • I want to get a Resource at runtime from the Application Resources, but at design time, I want to get it programmatically.

    I tried this (in RemObject's Oxygene language):

       constructor SiteTab; 
          if DesignerProperties.IsInDesignTool then begin
             if not Application.Current.Resources.Contains( 'LocalStrings' ) then
                Application.Current.Resources.Add( 'LocalStrings', new LocalStrings );

    But, after compiling, at design time the xaml still complains that the resource could not be resolved.

    Here is the xaml line:

          <TextBlock Text="{Binding Strings[Name], Source={StaticResource LocalStrings}, Converter={StaticResource EO_AddColon}}" x:Name="HookToLocalized"/>

    How can I do this?

    Wednesday, July 15, 2015 3:43 PM

All replies

  • Where is the resource merged usually?

    I would do that in a resource dictionary which is merged by app.xaml.

    Meaning it'll already be there when your usercontrol or whatever it is instantiates.

    You can overwrite the entry in application.current.resources.

    Application.Current.Resources["LocalStrings"] = whatever;

    Wednesday, July 15, 2015 4:56 PM
  • The question is for the xaml designer at design time.  The UserControl is in a TabControl on a Page that will be used by someone else's app using MEF later.  That person won't know about my pieces/parts so it won't be in that person's app resources.

    I tried both the Add technique and setting the Resources[ 'LocalStrings'] as you suggested in the constructor.  Neither seems to get the binding satisfied at design time.

    ... time passes ...

    I actually got an exception when I tried to do the Application.Current.Resources[ 'LocalStrings' ] := whatever syntax.  Only the Add worked at run time.

    • Edited by mltiede Wednesday, July 15, 2015 6:30 PM
    Wednesday, July 15, 2015 5:05 PM
  • Do have try design with Expression Blend?

    As I know, the value of DesignerProperties.IsInDesignTool is true in Expression Blend.

    Thursday, July 16, 2015 3:10 PM
  • There is no problem with the IsInDesignTool.

    I can't use Blend as it doesn't understand RemObject's Oxygene language.

    Thursday, July 16, 2015 3:15 PM
  • With wpf I've worked on a couple of apps didn't have a regular app.xaml.

    Nowhere to merge those default resource dictionaries.
    What I did was load these in application startup in code.

    To supply them at design time you can edit the csproj.


    Never tried it in Silverlight.

    Thursday, July 16, 2015 3:28 PM
  • I only want a Silverlight solution to get the resource set up so that it works at design time.

    I don't want them in the app project and there is no csproj since there is no C#.  But I know what you mean.

    Thursday, July 16, 2015 3:44 PM
  • I think it probably would work in Silverlight, or I wouldn't have suggested it.

    Are you not using visual studio?

    Isn't there some csproj equivalent which tells visual studio what's in a project and how to compile what?

    Thursday, July 16, 2015 4:20 PM
  • As I said, I know what you mean and I don't want to create resources in the app for various reasons.

    Yes, VS 2013 Pro.

    Yes, there is a project file and a solution.

    I'm trying to find a way to make a resource at design time in code behind of the UserControl.

    Apparently, no one can suggest a way that works.

    • Edited by Mark Tiede Thursday, July 16, 2015 5:21 PM
    Thursday, July 16, 2015 5:20 PM
  • Well maybe some lateral thinking then.

    SL5 added custom markup extensions.

    That always runs as code anyhow so you could have one thing at design time and another at run time.

    That isn't exactly what he does here, but it'll give you the idea if you're unfamiliar with them:


    Instead of:

        Text="{local:Resx MyKey=Whatever}" />

    I would have thought you could return a resource dictionary and merge it in the usercontrol resources

    Or just a converter if you could get away with binding one or two things.

    Thursday, July 16, 2015 6:00 PM
  • Yeah, I was looking at markup extensions, but I was hoping I wouldn't have to go that route.

    Not sure what you mean by "return a resource dictionary and merge it".  You mean in the code behind?  Return it to what?

    Thursday, July 16, 2015 6:14 PM
  • Return it to the usercontrol.

    Same way you merge a resource dictionary anywhere.

    You can merge in app.xaml and you can merge in a usercontrol.

    A markup extension can return anything so if you make it return an entire resource dictionary then you can xamlreader.load a text file or whatever.

            <ResourceDictionary Source="blaa.xaml" />
            <ResourceDictionary Source="{My markup extension}" />

    I guess you could inherit resource dictionary and extend it. Whack your code in to construct it and return a resource dictionary. Make it a staticresource itself.

    Then do

    <ResourceDictionary Source="{StaticResource MyResourceDictionary}" />

    Thursday, July 16, 2015 6:39 PM