AndyT
Topic Author
Posts: 3
Joined: 22 Jul 2015, 18:18

Reusable UserControls

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.
Parsing StaticResource (@27,4)
Can't add a 'StaticResourceExpression' to a 'UIElementCollection'
My xaml files are below...
<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>
and
<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>
What is the best way to achieve what I am trying to do?
Last edited by AndyT on 03 Aug 2015, 14:38, edited 1 time in total.
 
User avatar
jsantos
Site Admin
Posts: 2900
Joined: 20 Jan 2012, 17:18
Contact:

Re: Reusable UserControls

01 Aug 2015, 16:50

You don't need the ResourceDictionary. Once you create an UserControl, it can be used as a normal (like Button, Image) control.

Before digging into more details, did you read our tutorials about it?
 
AndyT
Topic Author
Posts: 3
Joined: 22 Jul 2015, 18:18

Re: Reusable UserControls

03 Aug 2015, 11:53

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...
<UserControl.Resources>
    <local:WobbleTile x:Key="WobbleTile"/>
</UserControl.Resources>
<Grid Grid.Row="0" Grid.Column="1">
    <StaticResource ResourceKey="WobbleTile"/>
</Grid>
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?
 
User avatar
sfernandez
Site Admin
Posts: 1911
Joined: 22 Dec 2011, 19:20

Re: Reusable UserControls

03 Aug 2015, 12:41

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
using UnityEngine;
using System;
using Noesis;

namespace GUI.UserControls
{
  [UserControlSource("Assets/GUI/UserControls/WobbleTile.xaml")
  public class WobbleTile : UserControl
  {
    ...
  }
} 
Assets/GUI/UserControls/WobbleTile.cs
<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> 
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):
<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>
We will improve the Unity tutorial to incorporate an example where the proposed ColorPicker user control is used.
 
AndyT
Topic Author
Posts: 3
Joined: 22 Jul 2015, 18:18

Re: Reusable UserControls

03 Aug 2015, 14:31

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!
 
User avatar
sfernandez
Site Admin
Posts: 1911
Joined: 22 Dec 2011, 19:20

Re: Reusable UserControls

03 Aug 2015, 15:23

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.
 
User avatar
sfernandez
Site Admin
Posts: 1911
Joined: 22 Dec 2011, 19:20

Re: Reusable UserControls

03 Aug 2015, 15:39

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.

Who is online

Users browsing this forum: Bing [Bot] and 1 guest