WPF StaticResource vs DynamicResource

In the process of creating any program, you’ll need to refactor.  As I’m a newbie with WPF and XAML, I’m still learning the in’s and out’s and came across an interesting issue with the designer while refactoring bits of the application to controls.

I have put some resources in my App.xaml.

<Application x:Class="Drunktender.Wpf.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    StartupUri="Window1.xaml">
    <Application.Resources>
		<Style x:Key="glowEffect" TargetType="TextBlock">
			<Setter Property="Foreground" Value="#fff" />
			<Setter Property="Effect">
				<Setter.Value>
					<DropShadowEffect ShadowDepth="0" Color="#39f" BlurRadius="4" />
				</Setter.Value>
			</Setter>
		</Style>
		<Style x:Key="glowEffectOnGlass" TargetType="Grid">
			<Setter Property="Effect">
				<Setter.Value>
					<DropShadowEffect ShadowDepth="0" Color="#fff" BlurRadius="5" />
				</Setter.Value>
			</Setter>
		</Style>
	</Application.Resources>
</Application>

And to reference them, you’ll typically do something like this.

<TextBlock Name="textBlock" FontSize="30" Style="{StaticResource glowEffect}">
	Clint is awesomer (true story).
</TextBlock>

Now this works great if you’re in a Window, not a user control.  As soon as this is moved into a user control you’ll get this ever so helpful error in Visual Studio’s design view.  Clicking Reload, no matter how many times will give the same error.

image

And in your error list, you’ll see the following: “Could not create an instance of type 'LoginScreen'.”

To fix this, change StaticResource to DynamicResource and recompile.  One thing also to pay attention to is the little things here as well.  I have a storyboard in a user control that made a StaticResource to itself, this too had to be shifted to a DynamicResource.  Note the “BeginStoryboard”

<UserControl x:Class="Drunktender.Wpf.Controls.LoginScreen"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
   <UserControl.Resources>
		<Storyboard x:Key="textGlowAnim" AutoReverse="True" RepeatBehavior="Forever">
			<DoubleAnimationUsingKeyFrames 
					BeginTime="00:00:00" 
					Storyboard.TargetName="textBlock" 
					Storyboard.TargetProperty="(UIElement.Effect).(DropShadowEffect.BlurRadius)">
				<SplineDoubleKeyFrame  KeyTime="00:00:01" Value="10"/>
			</DoubleAnimationUsingKeyFrames>
		</Storyboard>
	</UserControl.Resources>
	<UserControl.Triggers>
		<EventTrigger RoutedEvent="FrameworkElement.Loaded">
			<BeginStoryboard Storyboard="{DynamicResource textGlowAnim}"/>
		</EventTrigger>
	</UserControl.Triggers>
	<Grid>
		<TextBlock Name="textBlock" FontSize="30" Style="{DynamicResource glowEffect}">
			Use your card to log into the system
		</TextBlock>
	</Grid>
</UserControl>

I don’t know if this will fix everyone’s issue with this error all the time but it fixed mine.

Nicl Sep 5, 2009 @ 3:20 AM

# re: WPF StaticResource vs DynamicResource
Fixed my problems as well!!!
Thanks a LOT!

Clint Sep 6, 2009 @ 9:14 PM

# re: WPF StaticResource vs DynamicResource
@NICL Good to know it helped. If i find something that seems weird and not straight forward, I post about it.

Blasar Dec 9, 2009 @ 10:32 PM

# re: WPF StaticResource vs DynamicResource
A very well done article and explanatory.

emddudley Feb 4, 2010 @ 9:30 AM

# re: WPF StaticResource vs DynamicResource
Just be aware that this will have a small impact on the performance of your application. Really this is a bug in Visual Studio 2008's WPF designer--it should not treat missing StaticResources as errors, but as warnings. That way you can get the performance benefit of using StaticResources and still use the designer.

David Feb 4, 2010 @ 7:18 PM

# re: WPF StaticResource vs DynamicResource
Thank you. Solved my problem.

Post a Comment

Please add 3 and 4 and type the answer here: