co_nl_on
Topic Author
Posts: 11
Joined: 24 Jan 2015, 20:29

[RESOLVED] Can't name UserControl and find with FindName

03 Feb 2015, 10:28

To be clear, this is not a case where I want to find a child element of a UserControl. I've read the answers to that question already, and it's not something I'd want to do anyway. I'm just trying to access the UserControl itself. The UserControl is displaying just fine, I just can't access it via code. I made a super simple test case to show what's happening (obviously a real UserControl should provide more functionality).

Here's my UserControl, CustomLabel.xaml
<UserControl
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  x:Class="Controls.CustomLabel"
  UseLayoutRounding="True">
  <Label FontSize="50" Content="custom!"/>
</UserControl>
 
And CustomLabel.cs
using Noesis;
using Noesis.Samples;
using UnityEngine;
using System.Collections;

namespace Controls
{
    [Noesis.UserControlSource(@"Assets/CustomLabel.xaml")]
    public class CustomLabel : Noesis.UserControl
    {
    }
}
 
Here's my GUI where I have 2 CustomLabels and one plain old Label
<Grid
    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" Width="800" Height="600" Background="#2F2F2F" x:Name="Grid"
    xmlns:local="clr-namespace:Controls;assembly=Controls">
    <StackPanel x:Name="stackPanel">
        <local:CustomLabel x:Name="label1"/>
        <local:CustomLabel Name="labelWithNoX"/>
        <Label x:Name="normalLabel" Content="normal"/>
    </StackPanel>
</Grid>
 
And here's my MonoBehaviour... on the plus side it does display all the controls I intend it to.
using System;
using Controls;
using Noesis;
using UnityEngine;
using System.Collections;

public class GringosGui4 : MonoBehaviour
{
    private StackPanel stackPanel;


    // Use this for initialization
    void Start ()
    {
        stackPanel = this.GetComponent<NoesisGUIPanel>()
            .GetContent().FindName("stackPanel") as StackPanel;

        var customLabel = new CustomLabel();
        customLabel.Name = "codeLabel";

        stackPanel.Children.Add(customLabel);

        TryAccessLabel("label1"); // prints "label1 not found"
        TryAccessLabel("codeLabel"); // prints "codeLabel not found"
        TryAccessLabel("labelWithNoX"); // prints "labelWithNoX not found"
        TryAccessLabel("normalLabel"); // prints "normalLabel found successfully"
    }

    public void TryAccessLabel(string s)
    {
        var obj = stackPanel.FindName(s);

        if (obj == null)
        {
            Debug.LogError(s + " not found");
        }
        else
        {
            Debug.Log(s + " successfully found");
        }
    }
}
 
Now I expect this to print out that everything has been found successfully. All the labels are children of the StackPanel. They all have names which I'm looking up. But none of the CustomLabels are getting found.

EDIT: I can't reproduce this error with WPF either.
Last edited by co_nl_on on 04 Feb 2015, 10:57, edited 1 time in total.
 
User avatar
sfernandez
Site Admin
Posts: 2991
Joined: 22 Dec 2011, 19:20

Re: Can't name UserControl and find with FindName

03 Feb 2015, 19:16

Please download the latest version (1.2.0b8) because we solved an important bug that returns null references when accessing user controls.

I tried the test scene you posted with that version and it seems to work as expected.
 
co_nl_on
Topic Author
Posts: 11
Joined: 24 Jan 2015, 20:29

Re: Can't name UserControl and find with FindName

04 Feb 2015, 10:57

It worked!

Well, finding the code-inserted control didn't work, but I think I understand why it wouldn't.

Thanks for the speedy response.

Who is online

Users browsing this forum: Bing [Bot] and 12 guests