jc_lvngstn
Topic Author
Posts: 34
Joined: 23 Sep 2013, 03:03

Dynamically adding a usercontrol.

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.
<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>
Here is the xaml for my user control:
<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>
Again, pretty simple. Just a stackpanel with a button.

Here is the code behind for the main panel:
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());
	}
}
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.

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
 
User avatar
ai_enabled
Posts: 221
Joined: 18 Jul 2013, 05:28
Contact:

Re: Dynamically adding a usercontrol.

09 Oct 2013, 04:45

Looks like you forgot to point on class (x:Class="UserControls.TestWindow") in XAML:
    <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"
       x:Class="UserControls.TestWindow">
       <StackPanel>
          <Button>User Control!</Button>
       </StackPanel>
    </UserControl>
try to add this and click "Build" in Unity: menu Windows -> NoesisGUI -> Settings.
AtomicTorch Studio Pte. Ltd. http://atomictorch.com
 
jc_lvngstn
Topic Author
Posts: 34
Joined: 23 Sep 2013, 03:03

Re: Dynamically adding a usercontrol.

09 Oct 2013, 04:49

Oh my crap! It worked! I could have sworn I tried that and got partial class errors, but it worked :) :)

Thank you!
 
User avatar
jsantos
Site Admin
Posts: 2904
Joined: 20 Jan 2012, 17:18
Contact:

Re: Dynamically adding a usercontrol.

09 Oct 2013, 05:25

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.
The "Unity/Unity/" is internal and should never appear. It should say "Assets/_Game/..".

Will be fixed in the next release.
try to add this and click "Build" in Unity: menu Windows -> NoesisGUI -> Settings.
Hmm.. curious about this. Doesn't the xaml rebuild automatically when you edit it?
 
jc_lvngstn
Topic Author
Posts: 34
Joined: 23 Sep 2013, 03:03

Re: Dynamically adding a usercontrol.

10 Oct 2013, 05:16

Mine seems to rebuild consistently on its own.

By the way...I just want to say how excited I am with NoesisGUI so far. I've gotten my main canvas up, I'm dynamically adding windows, closing them, moving them around, etc. I'm very excited, and the level of support on this forum has definitely helped!
 
User avatar
ai_enabled
Posts: 221
Joined: 18 Jul 2013, 05:28
Contact:

Re: Dynamically adding a usercontrol.

10 Oct 2013, 06:14

try to add this and click "Build" in Unity: menu Windows -> NoesisGUI -> Settings.
Hmm.. curious about this. Doesn't the xaml rebuild automatically when you edit it?
Sometimes it's required to click build, sometimes not. We don't know why. And for our big project (~100 usercontrols) NoesisGUI build tool like to crash Unity in 75% of attempts (and Editor.log doesn't contain any error messages). We found workaround - force Unity to reimport folder with XAML's. It doesn't crash so often :-)...
Also we going crazy about Unity freezes and crashes when we stop play mode in Unity.
Hope the NoesisGUI 1.0.5 release will fix some of this issues.
AtomicTorch Studio Pte. Ltd. http://atomictorch.com
 
User avatar
jsantos
Site Admin
Posts: 2904
Joined: 20 Jan 2012, 17:18
Contact:

Re: Dynamically adding a usercontrol.

10 Oct 2013, 20:05

Mine seems to rebuild consistently on its own.

By the way...I just want to say how excited I am with NoesisGUI so far. I've gotten my main canvas up, I'm dynamically adding windows, closing them, moving them around, etc. I'm very excited, and the level of support on this forum has definitely helped!
Thanks! By the way, I sent you a private email.
 
User avatar
jsantos
Site Admin
Posts: 2904
Joined: 20 Jan 2012, 17:18
Contact:

Re: Dynamically adding a usercontrol.

10 Oct 2013, 20:11

Sometimes it's required to click build, sometimes not. We don't know why. And for our big project (~100 usercontrols) NoesisGUI build tool like to crash Unity in 75% of attempts (and Editor.log doesn't contain any error messages). We found workaround - force Unity to reimport folder with XAML's. It doesn't crash so often :-)...
Also we going crazy about Unity freezes and crashes when we stop play mode in Unity.
Hope the NoesisGUI 1.0.5 release will fix some of this issues.
Hi Vladimir, sorry to hear that. First notice I have that you are getting 75% of times crashes when you build. There is something totally wrong there. I have been using Unity + NoesisGUI for three days without having to reopen Unity. 0 crashes. I think that at your macroscopic scale (100+ usercontrols) new problems could be appearing. I wonder, could we (after signing a NDA) getting a copy of that project to investigate all your problems?

By the way, there won't be 1.0.5. Next version (we are finishing the last tests, if everything goes ok it will be in the servers tomorrow) is going to be 1.1.0 and brings a new and simplified API for UserControls. Now extending is a lot simpler. Apart from that and a lot more things, there are a few more bugfixed solved for you.

Please, remember, critical problems that go into our bugtracker receive high priority from our team.

Thanks
 
golgepapaz
Posts: 43
Joined: 01 Aug 2013, 01:59

Re: Dynamically adding a usercontrol.

11 Oct 2013, 09:17


By the way, there won't be 1.0.5. Next version (we are finishing the last tests, if everything goes ok it will be in the servers tomorrow) is going to be 1.1.0 and brings a new and simplified API for UserControls. Now extending is a lot simpler.

Thanks
Well that's a relief.. Eagerly anticipating. I am guessing the tutorials will be updated/rewritten accordingly?
 
User avatar
jsantos
Site Admin
Posts: 2904
Joined: 20 Jan 2012, 17:18
Contact:

Re: Dynamically adding a usercontrol.

11 Oct 2013, 12:38

Well that's a relief.. Eagerly anticipating. I am guessing the tutorials will be updated/rewritten accordingly?
Yes, tutorials have been updated.

Who is online

Users browsing this forum: Google [Bot] and 1 guest