[RESOLVED] Can't name UserControl and find with FindName
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
And CustomLabel.cs
Here's my GUI where I have 2 CustomLabels and one plain old Label
And here's my MonoBehaviour... on the plus side it does display all the controls I intend it to.
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.
Here's my UserControl, CustomLabel.xaml
Code: Select all
<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>
Code: Select all
using Noesis;
using Noesis.Samples;
using UnityEngine;
using System.Collections;
namespace Controls
{
[Noesis.UserControlSource(@"Assets/CustomLabel.xaml")]
public class CustomLabel : Noesis.UserControl
{
}
}
Code: Select all
<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>
Code: Select all
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");
}
}
}
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.
-
-
sfernandez
Site Admin
- Posts: 3222
- Joined:
Re: Can't name UserControl and find with FindName
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.
I tried the test scene you posted with that version and it seems to work as expected.
Re: Can't name UserControl and find with FindName
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.
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: Google [Bot], Semrush [Bot] and 2 guests