Skip to content
TheThing edited this page Mar 9, 2011 · 3 revisions

Binding objects into the NetworkLibrary is actually an easy task. When you register objects into the Network Library it automatically checks to see if it implements INotifyPropertyChanged. If the object does, then Network Library automatically listens on the object. The only task the programmer has to do, is make the object bindable to begin with. Once that's done, the only task left is to Register it into the library. If you know how to make objects implement INotifyPropertyChanged and know how it works then you don't need to read the rest of this documentation

Creating bindable objects

In order to make objects bindable we have to make it implement the INotifyPropertyChanged. This is done like this:

public class OurBindableObject : INetworkData, INotifyPropertyChanged
{
	//....
}

Now that this is done, we add the event PropertyChanged that the Network Library will listen on:

public event PropertyChangedEventHandler PropertyChanged;

That's it. Now for the tricky part. Each and every property has to check to see if somebody is listening on the event and if it is, notify about the change. I usually create a private method that helps with this and I am going to demonstrate this here:

private void RunPropertyChanged(string name)
{
	if (PropertyChanged != null)
		PropertyChanged(this, new PropertyChangedEventArgs(name));
}

For each and every property that has a set property, we run the method we just created with the name of the property:

private int _privateInt;

public int SomeIntValue
{
	get { return _privateInt; }
	set
	{
		_privateInt = value;
		RunPropertyChanged("SomeIntValue");
	}
}

Registering bind-able objects

After you have made an object bindable, no other necessary actions are necessary. The only thing left to do for you is to Register the object and the Network Library does the rest. No other actions are really necessary. The Network Library checks every object for supported type. Among those is the IValuePropertyChanged. If it encounters a supported object the Network Library does the supported action on it, in this case, Binding.

For more information on how to register the object into the library, check the advanced section, Registering Objects article for more information.

Disabling binding temporary

In some cases you might find yourself in a situation where you have to disable the binding on an object temporary while you do some modifications to it. If the property has binding but you don't want the changes to transmit you have 2 choices to disable the binding temporary. One is by calling ListenerDisable and ListenerEnable and the other method is calling the Safe method.

Using ListenerDisable and ListenerEnable can be simple but it puts the load on the programmer, you in other words. If you call ListenerDisable you have to remember to call ListenerEnable.

connection.NetworkDataHandler.ListenerDisable(ourObject);
ourObject.SomeProperty = 1234;
connection.NetworkDataHandler.ListenerEnable(ourObject);

The other method is to call the Safe method. Safe takes 2 parameters, one is a LINQ method where you can run your changes at will and the other is the object you would like to temporary disable while you run your LINQ method.

connection.NetworkDataHandler.Safe(() =>
{
	ourObject.SomeProperty = 1234;
}, ourObject);

You can also pass multiple objects to disable into the same Safe call. Let's say we have 2 Plots and we want to move the unit from plot 1 to plot 2, you can do this inside the Safe call like this:

connection.NetworkDataHandler.Safe(() =>
{
	targetPlot.Unit = sourcePlot.Unit;
	sourcePlot.Unit = null;
}, sourcePlot, targetPlot);

The Safe call automatically calls ListenerDisable for every object passed into the method. After the LINQ expression has finished running it calls ListenerEnable for every object. This is the recomennded method when such desirable actions are necessary since you don't have to worry about running ListenerEnable for every object you disabled.

Final words

That's it. It is very important that the name of the property is correct and does not contain any spelling mistake. The property is also case-sensitive so having a property named NumberOfUnits but running the method with the string "NumberofUnits" is not the same and will result in the property not being notified.

Now that you know this, why not give the Network Library a test drive.