-
-
Notifications
You must be signed in to change notification settings - Fork 337
Custom Platform Controls
Eto.Forms comes with many standard controls, however sometimes you may want to make full use of each platform's abilities, or if you want to re-use platform specific code already written. Custom platform controls can be created for this purpose.
Custom platform controls can have implementations for each platform, and are implemented exactly the way standard Eto.Forms controls are. You can either extend an existing control and provide a custom handler for each platform, or create a completely custom control.
All supportable Eto.Forms control handlers are registered through the Generator class. When creating a new Eto.Forms control (custom or not), the Generator looks up the control handler via it's interface type.
The drawback of creating custom platform controls is that you have to create a handler implementation of the control for each platform you wish to use it with.
This is an example of creating a custom text box control.
In your cross-platform library:
// interface to the platform implementations
public interface IMyCustomControl : IControl
{
string MyProperty { get; set; }
}
// control to use in your eto.forms code
public class MyCustomControl : Control
{
IMyCustomControl Handler { get { return (IMyCustomControl)base.Handler; } }
public MyCustomControl (Generator generator = null)
: base (generator, typeof (IMyCustomControl))
{
}
public string MyProperty
{
get { return Handler.MyProperty; }
set { Handler.MyProperty = value; }
}
}
In your MonoMac platform assembly:
public class MyCustomControlHandler : MacControl <NSTextField, MyCustomControl>, IMyCustomControl
{
public MyCustomControlHandler ()
{
this.Control = new NSTextField ();
}
public string MyProperty
{
get { return this.Control.StringValue; }
set { this.Control.StringValue = value ?? string.Empty; }
}
}
In your WPF assembly:
public class MyCustomControlHandler : WpfControl<Microsoft.Windows.Controls.TextBox, MyCustomControl>, IMyCustomControl
{
public MyCustomControlHandler ()
{
this.Control = new Microsoft.Windows.Controls.TextBox ();
}
public string MyProperty
{
get { return this.Control.Text; }
set { this.Control.Text = value; }
}
}
To register your new control, you must add it to the generator. This can be done before creating your application in your main() method.
var myGenerator = new Eto.Platform.Mac.Generator (); // mac generator
// to register your custom control handler, call this before using your class:
myGenerator.Add<IMyCustomControl>(() => new MyCustomControlHandler ());
// create application object, and run