Theming using resources without Blend vomitting

Go To StackoverFlow.com

3

WPF is great in that I should be able to use {StaticResource MyBackground} instead of "White" throughout my app, and then by changing resource definitions redefine the look of my entire application.

Problem is Expression Blend just won't work if you have references to global resources in a subcontrol. Is there any other way of theme-ing your app? I am not willing to lose Blend designer support, or replicate a piece of code/xaml in every single control. I find it astounding that they have not fixed a bug of this magnitude.

Any ideas?

Cheers

2012-04-03 19:55
by Harry Mexican
"Problem is Expression Blend just won't work if you have references to global resources in a subcontrol."

In what way does it "now work"? Where are your "global resources" located - David Nelson 2012-04-03 21:06

The designer can not locate the resources and thus does not diplay the controls. It's the bug reported in the link below. Hoping someone has found a better solution since then: http://stackoverflow.com/questions/2665988/mvvm-light-blend-designer-view-error-cannot-find-resource-named-locato - Harry Mexican 2012-04-03 22:06
It is my understanding that resources which are merged into App.xaml can be found by Blend with a StaticResource (confirmed at http://blogs.msdn.com/b/unnir/archive/2009/03/31/blend-wpf-and-resource-references.aspx). Is that where your "global resources" are located - David Nelson 2012-04-03 22:20
They can NOT be found with a StaticResource, but switching it to DynamicResource resolves the issue. Not sure what the performance penalty is. I came across that link a few minutes ago as well. See the answer below. Thanks for trying to solve it in any case - Harry Mexican 2012-04-03 22:22


0

Seems like this bug disappears if you use DynamicResources instead.

Explained here in b): http://blogs.msdn.com/b/unnir/archive/2009/03/31/blend-wpf-and-resource-references.aspx

Someone posted this link in an earlier SO question on the same bug, but for some reason it got no upvotes and was lost below other answers.

2012-04-03 22:20
by Harry Mexican
Note that there is a perf penalty to using Dynamic, but you can easily just switch them all to static for production deployments if it ends up being noticeable, as it's just to make Blend happy - Harry Mexican 2012-04-03 22:26


1

So I haven't tried this, but it sounds like it would work. Also, the information is a bit dated. Please do let me know if it solves the problem. :)

From the Blend blog, add the following two chunks to your project files:

<DesignTime Condition="'$(SolutionPath)'!='' AND Exists('$(SolutionPath)')">true</DesignTime>

…. 
<ApplicationDefinition Condition="'$(DesignTime)'=='true' AND '$(BuildingInsideVisualStudio)'!='true' AND '$(BuildingInsideExpressionBlend)'!='true'" Include="App.xaml"> 
      <Generator>MSBuild:Compile</Generator> 
      <SubType>Designer</SubType> 
</ApplicationDefinition>

Basically this includes your app.xaml conditionally into your project so it gets used at design time, but not at build time. I'm guessing they assume people use msbuild to produce production code rather than clicking the build compile button.

2012-04-03 23:43
by Mike Post
Where should I add the DesignTime - Lance 2013-10-03 03:21
Best guess: near the top of your csproj file - Mike Post 2013-10-03 16:28
Ads