jc_lvngstn
Topic Author
Posts: 34
Joined: 23 Sep 2013, 03:03

Identifying items in a User Control

13 Oct 2013, 20:31

I have this user control:
<UserControl
   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
   xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
   xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" mc:Ignorable="d" 
	x:Class="UserControls.UC_AbilityBar" >	
	
	<StackPanel Orientation="Horizontal">
		<Button>hey</Button>
	</StackPanel>
</UserControl>
I'm just trying to add items to the panel via code, but it keeps finding null as the value.

In the PostInit :
public void OnPostInit()
{
	_PanelAbilities = GetContent().As<StackPanel>();
}

I've tried this, and it doesn't identify the panel. I tried assigning a name to the panel, and it couldn't identify it that way also (I used FindName<StackPanel>("MyPanelName")).

So...any recommendations on this? And along that note...should I even be assigning a name to something in a usercontrol, where the name might not be unique (I may have many stackpanels in that user control, and many instances of that control.)

Thanks,
JC
 
User avatar
sfernandez
Site Admin
Posts: 2997
Joined: 22 Dec 2011, 19:20

Re: Identifying items in a User Control

14 Oct 2013, 11:24

Looking for UserControl named elements in the PostInit method is a common task, you can check it in the provided Primitives example (our ColorPicker user control is looking for some sliders there).

I just tried GetContent() and FindName() using the following code (compatible for 1.1.0 version) and it worked perfectly (it never returns null):
using UnityEngine;
using System;
using Noesis;

namespace UserControls
{
    [Noesis.Extended]
    [Noesis.UserControlSource("Assets/Tests/UserControls/UC_AbilityBar.xaml")]
    public class UC_AbilityBar : UserControl
    {
        public void OnPostInit()
        {
            {
                StackPanel content = GetContent().As<StackPanel>();
                if (content == null)
                {
                    Debug.Log("GetContent returned null");
                }
                else
                {
                    Debug.Log("GetContent return the StackPanel");
                }
            }
            {
                StackPanel content = FindName<StackPanel>("ContentPanel");
                if (content == null)
                {
                    Debug.Log("FindName returned null");
                }
                else
                {
                    Debug.Log("FindName return the StackPanel");
                }
            }
        }
    }
}
Please create a ticket in our bugtracker specifying NoesisGUI version, steps to reproduce and attach the required files, so we can test what is failing.

About UserControls, you should consider them as black boxes that offer some kind of features to the interface. Inside the UserControl you would name elements that are important for the logic of the user control itself, but outside it, they would mean nothing. Then you should expose properties describing the state of your UserControl that would be used by other parts of your UI design.

Who is online

Users browsing this forum: No registered users and 39 guests