[IronPython] Extending .NET Classes

Curt Hagenlocher curt at hagenlocher.org
Wed Apr 16 16:51:15 CEST 2008


On Wed, Apr 16, 2008 at 5:47 AM, Dan Eloff <dan.eloff at gmail.com> wrote:
> I think I've already guessed the answer to this one, but I want to
> hear it from someone else just to be thorough.
>
> There's no way to subclass, say Button, as SuperButton and then use
> <ipy:SuperButton> in XAML. Maybe if you involve some C#?

The best way to think of XAML is as an object serialization format.
The deserializer uses information about the assembly and class in
order to create the object at runtime.  Because a Python-defined class
can't currently exist as a CLR class inside a regular assembly,
there's no way for the deserializer to be able to construct an
instance of the correct type.

Conceptually, you could subclass Button using C# and then forward the
calls you're interested in to a Python object.  Then you'd use this
new class inside the XAML, along with a reference to the applicable
Python code.  But you'd always be dealing with two separate objects --
the one created by the deserializer and the one created by the DLR.

> Currently I've been able to get by in many cases simply by using
> composition. It looks a little strange to me, but I can use Button in
> the xaml, and
> have SuperButton(xaml.myButton) somewhere in the code. There are some
> things that are difficult to do that way, but so far it works for me.

That's probably because any code can hook the events on the Button;
you don't actually have to derive from Button to respond to its
events.

--
Curt Hagenlocher
curt at hagenlocher.org



More information about the Ironpython-users mailing list