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.
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.