Reusable UserControls
Posted: 31 Jul 2015, 18:53
I am trying to create a UserControl and then have instances of it within the cells of a grid.
My most successful attempt so far is using a ResourceDictionary. This works within Blend but does not work when it gets into Unity.
In Unity I get the following error in the xaml file at the point I try to use the "WobbleTile" resource.
My xaml files are below...
and
What is the best way to achieve what I am trying to do?
My most successful attempt so far is using a ResourceDictionary. This works within Blend but does not work when it gets into Unity.
In Unity I get the following error in the xaml file at the point I try to use the "WobbleTile" resource.
Code: Select all
Parsing StaticResource (@27,4)
Can't add a 'StaticResourceExpression' to a 'UIElementCollection'
Code: Select all
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<UserControl x:Key="WobbleTileKey" x:Name="WobbleTile">
<Grid>
<TextBlock Name="textBlock" FontStyle="Italic" TextWrapping="Wrap" Text="This is a WobbleTile" HorizontalAlignment="Center" VerticalAlignment="Center" Background="DarkGoldenrod" Foreground="AntiqueWhite"/>
</Grid>
</UserControl>
</ResourceDictionary>
Code: Select all
<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignWidth="640" d:DesignHeight="480">
<UserControl.Resources>
<ResourceDictionary Source="WobbleTile.xaml"/>
</UserControl.Resources>
<Viewbox VerticalAlignment="Center" HorizontalAlignment="Center">
<Grid HorizontalAlignment="Right" Margin="0,0,0,0" Height="200" Width="200">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*" />
<ColumnDefinition Width="1*" />
<ColumnDefinition Width="1*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="1*" />
<RowDefinition Height="1*" />
<RowDefinition Height="1*" />
</Grid.RowDefinitions>
<TextBlock Name="textBlock" Grid.Row="0" Grid.Column="0" FontStyle="Italic" TextWrapping="Wrap" Text="Here is some text" HorizontalAlignment="Center" VerticalAlignment="Center" Background="DodgerBlue" Foreground="AntiqueWhite"/>
<Grid Grid.Row="0" Grid.Column="1">
<StaticResource ResourceKey="WobbleTileKey"/>
</Grid>
</Grid>
</Viewbox>
</UserControl>