Reusable UserControls
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>
Last edited by AndyT on 03 Aug 2015, 14:38, edited 1 time in total.
Re: Reusable UserControls
OK, thanks.
I've had a look through the tutorial and changed my XAML to get rid of the ResourceDictionary.
To actually use the resource now I have the following snippets within my Grid UserControl...
Is that correct?
One area that I am still confused is what should be done in Blend, and what should be done in Unity.
I added my code-behind script WobbleTile.cs using Unity but this means that I get errors in Blend because it doesn't know about my code-behind class. Should the c# file be added in Blend as well?
I've had a look through the tutorial and changed my XAML to get rid of the ResourceDictionary.
To actually use the resource now I have the following snippets within my Grid UserControl...
Code: Select all
<UserControl.Resources>
<local:WobbleTile x:Key="WobbleTile"/>
</UserControl.Resources>
Code: Select all
<Grid Grid.Row="0" Grid.Column="1">
<StaticResource ResourceKey="WobbleTile"/>
</Grid>
One area that I am still confused is what should be done in Blend, and what should be done in Unity.
I added my code-behind script WobbleTile.cs using Unity but this means that I get errors in Blend because it doesn't know about my code-behind class. Should the c# file be added in Blend as well?
-
sfernandez
Site Admin
- Posts: 2984
- Joined:
Re: Reusable UserControls
Hi Andy,
Controls shouldn't be placed into Resources because an instance of a control can only have 1 logical parent. As Jesús pointed before, you just have to use your UserControl class as you would do with any other control provided by NoesisGUI.
For example, if you designed a WobbleTile user control:
Assets/GUI/UserControls/WobbleTile.xaml
Assets/GUI/UserControls/WobbleTile.cs
Then you can use it in any other XAML file like this (make sure you don't forget the namespace declaration (xmlns:local) to refer to the correct class):
We will improve the Unity tutorial to incorporate an example where the proposed ColorPicker user control is used.
Controls shouldn't be placed into Resources because an instance of a control can only have 1 logical parent. As Jesús pointed before, you just have to use your UserControl class as you would do with any other control provided by NoesisGUI.
For example, if you designed a WobbleTile user control:
Assets/GUI/UserControls/WobbleTile.xaml
Code: Select all
using UnityEngine;
using System;
using Noesis;
namespace GUI.UserControls
{
[UserControlSource("Assets/GUI/UserControls/WobbleTile.xaml")
public class WobbleTile : UserControl
{
...
}
}
Code: Select all
<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="GUI.UserControls.WobbleTile">
...
</UserControl>
Code: Select all
<Grid
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:GUI.UserControls">
<StackPanel>
<Button Content="Some button here"/>
<local:WobbleTile .../> <!-- an instance of your UserControl -->
<local:WobbleTile .../> <!-- another instance of your UserControl -->
...
</StackPanel>
</Grid>
Re: Reusable UserControls
Great!
I got that working using my own namespace rather than GUI.UserControls.
Just FYI, if I use the namespace you gave I get the following errors in Unity console...
Assets/Editor/NoesisGUI/NoesisAbout.cs(18,13): error CS0234: The type or namespace name 'Label' does not exist in the namespace 'GUI'. Are you missing an assembly reference?
Assets/Editor/NoesisGUI/NoesisAbout.cs(33,13): error CS0234: The type or namespace name 'skin' does not exist in the namespace 'GUI'. Are you missing an assembly reference?
Thanks for your help!
I got that working using my own namespace rather than GUI.UserControls.
Just FYI, if I use the namespace you gave I get the following errors in Unity console...
Assets/Editor/NoesisGUI/NoesisAbout.cs(18,13): error CS0234: The type or namespace name 'Label' does not exist in the namespace 'GUI'. Are you missing an assembly reference?
Assets/Editor/NoesisGUI/NoesisAbout.cs(33,13): error CS0234: The type or namespace name 'skin' does not exist in the namespace 'GUI'. Are you missing an assembly reference?
Thanks for your help!
-
sfernandez
Site Admin
- Posts: 2984
- Joined:
Re: Reusable UserControls
Yes, that GUI.UserControls namespace was just an example because I didn't know where you placed your class definition. You have to use in each case the appropriate namespace where your classes are defined.
-
sfernandez
Site Admin
- Posts: 2984
- Joined:
Re: Reusable UserControls
It seems that using GUI namespace in Unity turns the compiler crazy, so better avoid using it
I'm getting compiling errors in UnityTestTools scripts too if I use it.
I'm getting compiling errors in UnityTestTools scripts too if I use it.
Who is online
Users browsing this forum: Google [Bot] and 83 guests