I have a follow-up question on this topic. I managed to dynamic
load an xaml this way, however getting references of elements in the loaded xaml by name fails.
To clarify see the next files:
The container xaml
<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:noesis="clr-namespace:NoesisGUIExtensions" >
<Border x:Name="container" />
</UserControl>
The loaded xaml
<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:noesis="clr-namespace:NoesisGUIExtensions"
mc:Ignorable="d"
d:DesignHeight="400" d:DesignWidth="300">
<Grid>
<Border BorderThickness="2" Padding="10" >
<Grid x:Name="mainGrid">
<StackPanel x:Name="buttonPanel">
<TextBlock>Title</TextBlock>
<Button x:Name="Button1" Content="Button1" />
<Button x:Name="Button2" Content="Button2" />
</StackPanel>
</Grid>
</Border>
</Grid>
</UserControl>
Then I use the following code to load a xaml
public void Start()
{
var loaded = NoesisGUISystem.LoadXaml<UserControl>("Assets/PressIt.GUI/Test1.xaml");
gui = FindObjectOfType<NoesisGUIPanel>();
var root = gui.GetRoot<FrameworkElement>();
var container = root.FindName<Border>("container");
container.SetChild(loaded);
container.SetVisibility(Visibility.Visible);
button1= container.FindName<Button>("Button1");
button2 = container.FindName<Button>("Button2");
button1.Click += OnButton1Click; //<=== NULL reference exception
button2.Click += OnButton2Click;
}
The button1 and button2 variables are null, meaning that the FindName<Button>("...") failed. Note that the xaml works perfectly if I
load the xaml statically in NoesisGUIPanel.
Any ideas?