Weltista
Topic Author
Posts: 11
Joined: 14 Jun 2014, 23:52

[Unity] Binding to the nested properties.

22 Jun 2014, 17:29

Hi!

I'm trying to display the property that belongs not to the object set in DataContest itself, but rather the property of property of that object. In this case property prime_Target.object_ID of SpaceShip class object:
public class LocalObject: Noesis.BaseComponent {
...
	public string object_ID;
}

public class SpaceShip: LocalObject {
	private LocalObject _prime_Target;
	public LocalObject prime_Target {
		get { return _prime_Target; }
		set	{
			if (_prime_Target != value) {
				_prime_Target = value;				
				NotifyPropertyChanged("prime_Target");
			}
		}
	} 
}

public class ShipControlPanel : Noesis.UserControl {
	public ShipControlPanel(SpaceShip currentship) {
		SetDataContext(currentship);		
    }
}
But adding the following in XAML doesn't produce the expected results, the field remains empty after assigning new value to prime_Target.
 <TextBlock Text="{Binding prime_Target.object_ID}" Style="{StaticResource StatText}"/>	
What's worse, is that Unity freezes after exiting play mode. Any advise on how to correctly handle this would be appreciated :). Thanks!
 
User avatar
ai_enabled
Posts: 231
Joined: 18 Jul 2013, 05:28
Contact:

Re: [Unity] Binding to the nested properties.

22 Jun 2014, 19:26

Hello! As far as I know, you can't bind to public fields, only to properties. So, try to replace
public string object_ID;
with this line
public string object_ID { get; set; }
About Unity freezes - I'm also experiencing this problem. For me, killing UnityShaderCompiler process helps to "anti-freeze" Unity. I've reported issue, but Unity team seems to not pay much attention into it...
By the way, I'm fighting with Unity freezes for a long time (more than 1 year) - it's never was stable, especially if you use native plugins, threading, sockets, etc. So, instead of stopping "Play mode", I'm prefer to use this AutoHotkey shortcut (Windows+Shift+E) to restart Unity Editor:
#+e::
Process, Close, Unity.exe
Run C:\Program Files (x86)\Unity\Editor\Unity.exe
AtomicTorch Studio Pte. Ltd. http://atomictorch.com
 
Weltista
Topic Author
Posts: 11
Joined: 14 Jun 2014, 23:52

Re: [Unity] Binding to the nested properties.

23 Jun 2014, 15:21

Hello ai!

I adjusted code as you advised, but GUI still doesn't show this data. So I guess it wasn't the only problem or maybe this feature is not [correctly] implemented in Noesis plugin. It would be great to hear developers opinion on the matter.
Thanks for your advice anyway -- at least there is one error less in my scripts :).

Regarding freezes -- it's really annoying indeed. The Editor behavior is rather unpredictable ATM. It may return normally in edit mode after I spend quite a time in play mode trying various features, and yet it can freeze after entering and exiting play mode for a few seconds.

Killing UnityShaderCompiler process helping though, so thanks for the tip. And for the shortcut -- I hope I won't get used to it).
 
User avatar
ai_enabled
Posts: 231
Joined: 18 Jul 2013, 05:28
Contact:

Re: [Unity] Binding to the nested properties.

23 Jun 2014, 16:08

DataContext binding should work for this case:
<TextBlock DataContext="{Binding prime_Target}" Text="{Binding object_ID}" Style="{StaticResource StatText}"/>
Also I recommend you to use NotifyPropertyChanged() at the LocalObject.object_id property to provide updates to the NoesisGUI:
private string _object_ID;
public string object_ID
{
    get { return _object_ID; }
    set
    {
        if (_object_ID != value)
        {
             _object_ID = value;
             NotifyPropertyChanged("object_ID");
        }
    }
}
Hope it helps!
AtomicTorch Studio Pte. Ltd. http://atomictorch.com
 
Weltista
Topic Author
Posts: 11
Joined: 14 Jun 2014, 23:52

Re: [Unity] Binding to the nested properties.

23 Jun 2014, 17:55

Also I recommend you to use NotifyPropertyChanged() at the LocalObject.object_id property to provide updates to the NoesisGUI:
Got it, but I can't do it ATM since LocalObject is a parent class, and there is a bug with NotifyPropertyChanged() events declared in parent classes: viewtopic.php?f=3&t=435

Changing TexBlock description in xaml as you suggested didn't help unfortunately. But thanks for trying to help anyways! Cheers).
 
User avatar
sfernandez
Site Admin
Posts: 2983
Joined: 22 Dec 2011, 19:20

Re: [Unity] Binding to the nested properties.

27 Jun 2014, 16:41

Hi!

I'm trying to display the property that belongs not to the object set in DataContest itself, but rather the property of property of that object. In this case property prime_Target.object_ID of SpaceShip class object:

...

But adding the following in XAML doesn't produce the expected results, the field remains empty after assigning new value to prime_Target.
Hi,

First of all you need to add the [Noesis.Extended] attribute to your classes, so we can register correctly in our system. The UserControl must also have the [Noesis.UserControlSource("<path_to_the_usercontrol_xaml_here>")] attibute, so we can load the associated xaml when your UserControl is created.

Next, as @ai_enabled suggested, bindings only work when used with C# properties, it won't work with simple fields.

I modified your code to the following, and it is working fine:
[Noesis.Extended]
public class LocalObject : Noesis.BaseComponent {
    public string object_ID { get; set; }
}

[Noesis.Extended]
public class SpaceShip : LocalObject {
    private LocalObject _prime_Target;
    public LocalObject prime_Target {
        get { return _prime_Target; }
        set
        {
            if (_prime_Target != value) {
                _prime_Target = value;
                NotifyPropertyChanged("prime_Target");
            }
        }
    }
}

[Noesis.Extended]
[Noesis.UserControlSource("Assets/NoesisGUI/Samples/_Tests/_Test/_Test.xaml")]
public class ShipControlPanel : Noesis.UserControl {
    public ShipControlPanel(SpaceShip currentship) {
        SetDataContext(currentship);      
    }
}
What's worse, is that Unity freezes after exiting play mode. Any advise on how to correctly handle this would be appreciated :). Thanks!
Maybe your freezing problems are related to what is explained in this topic: viewtopic.php?f=3&t=92

Let me know if killing adb.exe process solves it.
 
Weltista
Topic Author
Posts: 11
Joined: 14 Jun 2014, 23:52

Re: [Unity] Binding to the nested properties.

28 Jun 2014, 21:10

Hi,
Thanks for the tips, it's actually working now, at least to some degree :? .

Thing is that in order to make it work, you have to assign 'non-null' value to corresponding property before calling 'ContentControl.SetContent' , otherwise it won't show the property in GUI.

So, the following code only works if you add myShip.prime_Target = myTargetOne before
_controlPanelContainer.SetContent(ControlPanel).
    void Start()
    {
        var gui = GetComponent<NoesisGUIPanel>();
		root = gui.GetRoot<Noesis.FrameworkElement>();
		this._controlPanelContainer = root.FindName<Noesis.ContentControl>("controlPanelContainer");

		myShip = new SpaceShip();
		myTargetOne = new LocalObject ();
		myTargetOne.Object_ID = "Hi brothers!";

		myTargetTwo = new LocalObject ();
		myTargetTwo.Object_ID = "Bye brothers!";

		var ControlPanel = new ControlPanel(myShip);

		myShip.prime_Target = myTargetOne; // w/o this line it won't show myShip.prime_Target.Object_ID

		this._controlPanelContainer.SetContent(ControlPanel);
    }

	void Update () {
		if ( Mathf.Repeat( Time.time, 1f) > 0.5f) myShip.prime_Target = myTargetOne;
		else myShip.prime_Target = myTargetTwo;
	}
}
Just in case -- repro scene is attached.

EDIT:
Eventually setting reference property value to null at any pont in time breaks the binding. Thus the following wouldn't work after 3 seconds:
 void Update () {
		if ( Mathf.Repeat( Time.time, 1f) > 0.5f) myShip.prime_Target = myTargetOne;
		else myShip.prime_Target = myTargetTwo;
		if ( Time.time > 3f && Time.time < 3.05f) myShip.prime_Target = null;
	} 
Attachments
Sample.rar
(8.71 KiB) Downloaded 210 times
 
User avatar
sfernandez
Site Admin
Posts: 2983
Joined: 22 Dec 2011, 19:20

Re: [Unity] Binding to the nested properties.

03 Jul 2014, 19:20

Thing is that in order to make it work, you have to assign 'non-null' value to corresponding property before calling 'ContentControl.SetContent' , otherwise it won't show the property in GUI.
Eventually setting reference property value to null at any pont in time breaks the binding. Thus the following wouldn't work after 3 seconds
Both issues have been solved in 1.1.9 version.
I tried your sample and is working fine in any scenario you displayed.

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot], vinick and 16 guests