Dynamically adding a usercontrol.
Posted: 09 Oct 2013, 03:57
I'm having trouble dynamically adding a user control to an existing panel.
I have the main panel defined below. All it is is a stackpanel names WindowList. I want to add the user control to this stackpanel.
Here is the xaml for my user control:
Again, pretty simple. Just a stackpanel with a button.
Here is the code behind for the main panel:
You can see that I am executing ShowInventory() when the "Show UserControl" button is clickked. It's adding it to the _WindowList (the stackpanel in the main UI) children. By the way, the Debug is correctly showing that the stackpanel is being identified correctly in my output.
Here is the code for the TestWindow usercontrol.
What happens when I actually click on the Add UserControl button is this:
ApplicationException: Unity/Unity/_Game/NoesisUI/TestWindow.xaml resource not found
Noesis.Collection.Add (Noesis.BaseComponent item) (at Assets/Plugins/NoesisGUI/Scripts/Proxies/Collection.cs:90)
TestController.ShowInventory () (at Assets/_Game/NoesisUI/TestController.cs:35)
TestController.ShowInvButtonOnClick (Noesis.BaseComponent arg0, Noesis.RoutedEventArgs routedEventArgs) (at Assets/_Game/NoesisUI/TestController.cs:30)
Noesis.BaseButton.RaiseClick (IntPtr cPtr, IntPtr arg0, IntPtr arg1) (at Assets/Plugins/NoesisGUI/Scripts/Proxies/BaseButton.cs:102)
(wrapper native-to-managed) Noesis.BaseButton:RaiseClick (intptr,intptr,intptr)
Noesis.UIRenderer.Noesis_MouseButtonUp (Int32 rendererId, Single x, Single y, Int32 button) (at Assets/Plugins/NoesisGUI/Scripts/Core/NoesisUIRendererImports.cs:168)
Noesis.UIRenderer.ProcessEvent (UnityEngine.Event ev) (at Assets/Plugins/NoesisGUI/Scripts/Core/NoesisUIRenderer.cs:205)
NoesisGUIPanel.OnGUI () (at Assets/Plugins/NoesisGUI/Scripts/NoesisGUIPanel.cs:134)
It's odd that it shows it is looking in Unity/Unity/_Game/Noes... location. I have verified that the usercontrol is in the correct folder.
Any thoughts/suggestions on this? If this is a bit confusing to decipher, a small, simple example of how to have a single main panel, with dynamically added user controls would be very useful. It doesn't have to be complex, just very simple controls for speed's sake
Thank you,
JC
I have the main panel defined below. All it is is a stackpanel names WindowList. I want to add the user control to this stackpanel.
Code: Select all
<DockPanel
Name="ContainerDockPanel"
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" xmlns:UserControls="clr-namespace:UserControls" mc:Ignorable="d" MaxWidth="500" Height="300" MinHeight="110">
<DockPanel.RenderTransform>
<TranslateTransform X="0" Y="0"/>
</DockPanel.RenderTransform>
<StackPanel Name="WindowList">
<Button Name="ShowUserControl">Show UserControl</Button>
</StackPanel>
</DockPanel>Code: Select all
<UserControl Name="TestWindow" 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">
<StackPanel>
<Button>User Control!</Button>
</StackPanel>
</UserControl>Here is the code behind for the main panel:
Code: Select all
public class TestController : MonoBehaviour
{
private NoesisGUIPanel _uiPanel;
private FrameworkElement _Root;
private Button _ShowInvButton;
private StackPanel _WindowList;
private DockPanel _DockPanel;
// Use this for initialization
private void Start()
{
_uiPanel = NoesisGUISystem.GetPanel("TestController");
_Root = _uiPanel.GetRoot<FrameworkElement>();
_DockPanel = _Root.FindName<DockPanel>("ContainerDockPanel");
_ShowInvButton = _Root.FindName<Button>("ShowUserControl");
Debug.Log(_ShowInvButton.GetName());
_ShowInvButton.Click += ShowInvButtonOnClick;
_WindowList = _DockPanel.FindName<StackPanel>("WindowList");
Debug.Log(_WindowList.GetName());
}
private void ShowInvButtonOnClick(BaseComponent arg0, RoutedEventArgs routedEventArgs)
{
ShowInventory();
}
private void ShowInventory()
{
_WindowList.GetChildren().Add(new TestWindow());
}
}Here is the code for the TestWindow usercontrol.
Code: Select all
namespace UserControls
{
public class TestWindow : UserControl
{
private HandleRef swigCPtr;
public static void Register()
{
// Override the source to indicate the user control xaml
UserControl.SourceProperty.OverrideMetadata(typeof(TestWindow),
new PropertyMetadata("Assets/_Game/NoesisUI/TestWindow.xaml"));
}
public TestWindow(IntPtr cPtr, bool cMemoryOwn)
: base(cPtr, cMemoryOwn)
{
swigCPtr = new HandleRef(this, cPtr);
}
public TestWindow()
: this(Noesis.Extend.New(typeof(TestWindow)), true)
{
Noesis.Extend.Register(typeof(TestWindow), swigCPtr.Handle, this);
}
public override void Dispose()
{
lock (this)
{
if (swigCPtr.Handle != IntPtr.Zero)
{
if (swigCMemOwn)
{
swigCMemOwn = false;
if (Kernel.IsInitialized())
{
Noesis.Extend.Delete(typeof(TestWindow), swigCPtr.Handle);
}
}
swigCPtr = new HandleRef(null, IntPtr.Zero);
}
GC.SuppressFinalize(this);
base.Dispose();
}
}
}
}What happens when I actually click on the Add UserControl button is this:
ApplicationException: Unity/Unity/_Game/NoesisUI/TestWindow.xaml resource not found
Noesis.Collection.Add (Noesis.BaseComponent item) (at Assets/Plugins/NoesisGUI/Scripts/Proxies/Collection.cs:90)
TestController.ShowInventory () (at Assets/_Game/NoesisUI/TestController.cs:35)
TestController.ShowInvButtonOnClick (Noesis.BaseComponent arg0, Noesis.RoutedEventArgs routedEventArgs) (at Assets/_Game/NoesisUI/TestController.cs:30)
Noesis.BaseButton.RaiseClick (IntPtr cPtr, IntPtr arg0, IntPtr arg1) (at Assets/Plugins/NoesisGUI/Scripts/Proxies/BaseButton.cs:102)
(wrapper native-to-managed) Noesis.BaseButton:RaiseClick (intptr,intptr,intptr)
Noesis.UIRenderer.Noesis_MouseButtonUp (Int32 rendererId, Single x, Single y, Int32 button) (at Assets/Plugins/NoesisGUI/Scripts/Core/NoesisUIRendererImports.cs:168)
Noesis.UIRenderer.ProcessEvent (UnityEngine.Event ev) (at Assets/Plugins/NoesisGUI/Scripts/Core/NoesisUIRenderer.cs:205)
NoesisGUIPanel.OnGUI () (at Assets/Plugins/NoesisGUI/Scripts/NoesisGUIPanel.cs:134)
It's odd that it shows it is looking in Unity/Unity/_Game/Noes... location. I have verified that the usercontrol is in the correct folder.
Any thoughts/suggestions on this? If this is a bit confusing to decipher, a small, simple example of how to have a single main panel, with dynamically added user controls would be very useful. It doesn't have to be complex, just very simple controls for speed's sake
Thank you,
JC