Logan
Topic Author
Posts: 1
Joined: 21 Aug 2015, 01:39

Accessing objects inside a tab control

23 Aug 2015, 06:21

Hi everyone,

Full disclosure first of all I am brand new to using Noesis so my XAML's and code is rough to say the least. I am having some trouble accessing some XAML objects that are buried deep in the hierarchy. I dont have much challenge accessing stuff outside of my tab control like this:
NoesisGUIPanel noesisGUI = GetComponent<NoesisGUIPanel>();
		Noesis.FrameworkElement root = noesisGUI.GetContent();

		// Store references to important elements for future use
		waterTankFillBox = (Noesis.Rectangle)root.FindName("WaterTankFill");
		fillText = (Noesis.TextBlock)root.FindName ("WaterFillText");
		gearIcon = (Noesis.Path)root.FindName ("LandingGearIcon");
		fuelBar = (Noesis.Rectangle)root.FindName("FuelBar");
		inGameMenu = (Noesis.Grid)root.GetTemplateChild("GameMenu");
and that works great. However once I try to access something in the hierarchy which is in my tab controller its NULL REFERENCE EXCEPTIONS pouring out the walls. My XAML looks like this:
<UserControl 
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:Assets.GUI"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300" Width="1600" Height="900" HorizontalAlignment="Center" VerticalAlignment="Center">
    <Viewbox>
        <Grid Height="720" Width="1280">
            <Grid x:Name="Water" HorizontalAlignment="Left" Height="316" Margin="-13.982,0,0,5" VerticalAlignment="Bottom" Width="98.486">
                <Rectangle x:Name="WaterTankBase" Fill="#FF2B333B" Margin="18.982,16,19.504,0" Stroke="Black" RadiusX="10" RadiusY="10"/>
                <TextBlock x:Name="textBlock" Height="16" Margin="18.982,0,19.504,0" TextWrapping="Wrap" VerticalAlignment="Top" TextAlignment="Center" Foreground="White"><Run Text="WATER"/><LineBreak/><Run/></TextBlock>
                <Rectangle x:Name="WaterTankFill" Fill="Blue" Margin="23.982,21,24.504,5" Stroke="Black" RadiusX="10" RadiusY="10" VerticalAlignment="Bottom" Height="290"/>
                <TextBlock x:Name="WaterFillText" Margin="0,156.854,0,130.313" TextWrapping="Wrap" Text="XXXL" FontSize="21.333" TextAlignment="Center" RenderTransformOrigin="0.491,0.498">
                    <TextBlock.RenderTransform>
                        <TransformGroup>
                            <ScaleTransform/>
                            <SkewTransform/>
                            <RotateTransform Angle="-89.775"/>
                            <TranslateTransform X="0.825" Y="0.943"/>
                        </TransformGroup>
                    </TextBlock.RenderTransform>
                </TextBlock>
            </Grid>
            <Path x:Name="LandingGearIcon" Data="M20,44.000082 C21.104571,44.000082 22,44.895512 22,46.000082 22,47.104652 21.104571,48.000082 20,48.000082 18.89543,48.000082 18,47.104652 18,46.000082 18,44.895512 18.89543,44.000082 20,44.000082 z M20.25,36.375082 C15.003295,36.375082 10.75,40.628377 10.75,45.875082 10.75,51.121788 15.003295,55.375082 20.25,55.375082 25.496706,55.375082 29.75,51.121788 29.75,45.875082 29.75,40.628377 25.496706,36.375082 20.25,36.375082 z M20,26.500082 C30.769552,26.500082 39.5,35.23053 39.5,46.000082 39.5,56.769634 30.769552,65.500082 20,65.500082 9.230448,65.500082 0.50000021,56.769634 0.5,46.000082 0.50000021,35.23053 9.230448,26.500082 20,26.500082 z M19.000159,0.5 L21.082186,0.50001526 C22.14093,0.50002289 22.99922,1.358305 22.999228,2.4170399 L22.999268,7.8287163 26.654211,1.6483002 C27.193133,0.7369957 28.368786,0.43512917 29.280104,0.97406006 L30.140009,1.4825859 C31.051329,2.0215187 31.353216,3.1971664 30.814295,4.108469 L23.1794,17.018885 C23.128876,17.104321 23.072756,17.184399 23.011782,17.258929 L22.999338,17.273384 22.999378,22.583149 C22.999386,23.641885 22.141112,24.500154 21.082367,24.500146 L19.000338,24.50013 C17.941595,24.500121 17.083306,23.64184 17.083298,22.583105 L17.083147,2.416996 C17.08314,1.3582611 17.941414,0.49999237 19.000159,0.5 z" Fill="#FF2B333B" HorizontalAlignment="Left" Height="66" Margin="70,0,0,10" Stretch="Fill" Stroke="Black" VerticalAlignment="Bottom" Width="40"/>
            <Grid x:Name="FuelIndicator" HorizontalAlignment="Right" Height="154.667" Margin="0,0,10,10" VerticalAlignment="Bottom" Width="45.201">
                <Rectangle x:Name="FuelBar" Fill="Red" HorizontalAlignment="Right" Stroke="Red" Width="12" Height="150" VerticalAlignment="Bottom"/>
                <Path x:Name="F" Data="M0.5,0.5 L4.5000001,0.5 4.5000001,0.51981187 12.701179,0.5369885 12.692801,4.5369797 4.5000001,4.5198207 4.5000001,7.9831424 9.2056819,7.9929981 9.1973048,11.99299 4.5000001,11.983151 4.5000001,19.5 0.5,19.5 z" Fill="White" HorizontalAlignment="Left" Height="20" Stretch="Fill" Stroke="Black" VerticalAlignment="Top" Width="13.201"/>
                <Path x:Name="E" Data="M0.5,0.5 L4.5000001,0.5 4.5000001,0.51981175 12.701179,0.5369885 12.692801,4.5369797 4.5000001,4.5198207 4.5000001,7.9831424 9.2056819,7.9929981 9.1973048,11.99299 4.5000001,11.983151 4.5000001,15.470649 12.301178,15.486988 12.292801,19.48698 4.5000001,19.470659 4.5000001,19.5 0.5,19.5 z" Fill="White" HorizontalAlignment="Left" Height="20" Stretch="Fill" Stroke="Black" VerticalAlignment="Bottom" Width="13.201"/>
                <Rectangle x:Name="Dash" Fill="White" Margin="18.201,72.734,17,76.933" Stroke="Black"/>
                <Rectangle x:Name="Dash1" Fill="White" Height="5" Margin="18.201,7.467,17,0" Stroke="Black" VerticalAlignment="Top"/>
                <Rectangle x:Name="Dash2" Fill="White" Height="5" Margin="18.201,0,17,7.2" Stroke="Black" VerticalAlignment="Bottom"/>
            </Grid>
            <Grid x:Name="GameMenu" Margin="518.134,125.2,461.866,94.8" HorizontalAlignment="Center" VerticalAlignment="Center">

                <Rectangle x:Name="MenuCenterPane" Fill="#FF2B333B" Stroke="Black" Width="300" Height="500" RadiusY="28.5" RadiusX="28.5"/>
                <TextBlock x:Name="MenuText" Margin="10,10,10,0" TextWrapping="Wrap" Text="Menu" VerticalAlignment="Top" TextAlignment="Center" FontSize="29.333"/>
                <Button x:Name="ExitButton" Content="EXIT GAME" Height="60.667" Margin="10,0,10,25" VerticalAlignment="Bottom" HorizontalAlignment="Center" FontSize="32" Width="280"/>
                <Button x:Name="GameOptions" Content="Game Options" Margin="10,0,10,156.334" FontSize="32" Height="60.667" VerticalAlignment="Bottom"/>
                <Button x:Name="StoreButton" Content="Store" Margin="10,54.017,10,0" FontSize="32" Height="60.667" VerticalAlignment="Top"/>
                <Button x:Name="RestartButton" Content="Restart Game" Height="60.667" Margin="10,0,10,90.667" VerticalAlignment="Bottom" HorizontalAlignment="Center" FontSize="32" Width="280"/>

            </Grid>
            <Grid x:Name="StoreGrid" Margin="263.499,125.2,216.5,94.8" Width="800" HorizontalAlignment="Center" VerticalAlignment="Center" Height="500" Background="#FF2B333B" d:IsHidden="True">
                <TabControl x:Name="StoreTabs" FontSize="16">
                    <TabItem Header="Aircraft Settings">
                        <Grid Background="#FF2B333B">
                            <GroupBox x:Name="StatsBlock" Header="Status" HorizontalAlignment="Left" Margin="10,10,0,10" Width="250" FontSize="16">
                                <Grid Margin="0">
                                    <GroupBox x:Name="FuelBox" Header="Fuel:" Height="55" VerticalAlignment="Top">
                                        <TextBlock x:Name="FuelText" Margin="4,1.44,0,-6" TextWrapping="Wrap" Text="XXXL/XXXL" HorizontalAlignment="Center" d:LayoutOverrides="Height" VerticalAlignment="Center"/>
                                    </GroupBox>
                                    <GroupBox x:Name="WeightBox" Header="Weight:" Height="127" Margin="0,60,0,0" VerticalAlignment="Top">
                                        <Grid Margin="0">
                                            <TextBlock x:Name="TotalWeight" Margin="10,4.001,0,0" TextWrapping="Wrap" Text="Total:" VerticalAlignment="Top" Width="216"/>
                                            <TextBlock x:Name="FuelWeight" Margin="10,25.281,0,0" TextWrapping="Wrap" Text="Fuel:" d:LayoutOverrides="VerticalAlignment" VerticalAlignment="Top" Width="216"/>
                                            <TextBlock x:Name="WaterWeight" Margin="10,46.561,0,0" TextWrapping="Wrap" Text="Water:" Width="216" VerticalAlignment="Top"/>
                                            <TextBlock x:Name="AircarftWeight" Margin="10,0,0,9.599" TextWrapping="Wrap" Text="Aircraft:" VerticalAlignment="Bottom"/>
                                        </Grid>
                                    </GroupBox>
                                    <GroupBox x:Name="WaterBox" Header="Water" Margin="0,192,0,0">
                                        <Grid Margin="0,-0.44,-2,2.4">
                                            <TextBlock x:Name="WaterVolume" Height="26.4" Margin="10,46.2,5,0" TextWrapping="Wrap" Text="Current Water: XXXL" VerticalAlignment="Top"/>
                                            <Slider x:Name="AmoutToDropSlider" Margin="10,23.2,5,0" VerticalAlignment="Top" Width="213"/>
                                            <TextBlock x:Name="AmoutToDropText" Height="23.2" Margin="10,0" TextWrapping="Wrap" Text="Volume To Drop: XXXL" VerticalAlignment="Top"/>
                                            <TextBlock x:Name="MaximumWaterVolume" Margin="10,72.6,10,0" TextWrapping="Wrap" Text="Maximum Water: XXXL" VerticalAlignment="Top"/>
                                        </Grid>
                                    </GroupBox>
                                </Grid>
                            </GroupBox>
                        </Grid>
                    </TabItem>
                    <TabItem Header="Upgrade Store">
                        <Grid Background="#FFE5E5E5"/>
                    </TabItem>
                </TabControl>
            </Grid>
        </Grid>
    </Viewbox>
</UserControl>
So, To access that stuff in the tab control what should my code look like?
Thanks in advance!
 
User avatar
sfernandez
Site Admin
Posts: 3154
Joined: 22 Dec 2011, 19:20

Re: Accessing objects inside a tab control

25 Aug 2015, 18:42

Hi and welcome to NoesisGUI community! :)

I've tried to look for all the elements inside the TabControl and I can find them without problem:
public class FindNameBehavior: MonoBehaviour
{
    void Start()
    {
        var gui = GetComponent<NoesisGUIPanel>();
        var content = gui.GetContent(); // content is the root UserControl you posted

        var StoreTabs = (Noesis.TabControl)content.FindName("StoreTabs"); Dump(StoreTabs);
        var StatsBlock = (Noesis.GroupBox)content.FindName("StatsBlock"); Dump(StatsBlock);
        var FuelBox = (Noesis.GroupBox)content.FindName("FuelBox"); Dump(FuelBox);
        var FuelText = (Noesis.TextBlock)content.FindName("FuelText"); Dump(FuelText);
        var WeightBox = (Noesis.GroupBox)content.FindName("WeightBox"); Dump(WeightBox);
        var TotalWeight = (Noesis.TextBlock)content.FindName("TotalWeight"); Dump(TotalWeight);
        var FuelWeight = (Noesis.TextBlock)content.FindName("FuelWeight"); Dump(FuelWeight);
        var WaterWeight = (Noesis.TextBlock)content.FindName("WaterWeight"); Dump(WaterWeight);
        var AircarftWeight = (Noesis.TextBlock)content.FindName("AircarftWeight"); Dump(AircarftWeight);
        var WaterBox = (Noesis.GroupBox)content.FindName("WaterBox"); Dump(WaterBox);
        var WaterVolume = (Noesis.TextBlock)content.FindName("WaterVolume"); Dump(WaterVolume);
        var AmoutToDropSlider = (Noesis.Slider)content.FindName("AmoutToDropSlider"); Dump(AmoutToDropSlider);
        var AmoutToDropText = (Noesis.TextBlock)content.FindName("AmoutToDropText"); Dump(AmoutToDropText);
        var MaximumWaterVolume = (Noesis.TextBlock)content.FindName("MaximumWaterVolume"); Dump(MaximumWaterVolume);
    }

    void Dump(FrameworkElement element)
    {
        if (element != null)
        {
            Debug.Log(string.Format("{0} ({1})", element.GetType().Name, element.Name));
        }
        else
        {
            Debug.Log("null");
        }
    }
} 
No null is dumped to the console.

What function are you using to look for the elements inside the TabControl?

Who is online

Users browsing this forum: No registered users and 1 guest