I tried:

import System
import System.Windows

@clr.clrmethod([System.object, System.Windows.RoutedEventArgs])
def OnStartButton(sender,args):
    #do stuff
    pass

and get: ‘Cannot perform runtime binding on a null referenceAttributeError : 
object’

So I tried subscribing to the Click event from the Python end and that works. 
However, that’s not so good for me so I tried subclassing Button in C# and 
debugging what is passed to the Click event when subscribing from Python and 
sure enough, it is a RoutedEventhandler (even without the @clr.crlmethod 
decorator). There must be a way to pass-in the method to C#!!

Background: this is an application to support simple GUI development in Python. 
The C# end handles creating and displaying the GUI components which results in 
very simple Python:

# works in IronPython
b = app.Button("Start", OnStartButton)

# can’t use OnStartButton as argument, have to add this in Python.NET :-(
b.Click += OnStartButton

By the way I’m using Python.Net with PyScope from my own build.

Steve

From: PythonDotNet 
[mailto:pythondotnet-bounces+steve.ward=oxinst....@python.org] On Behalf Of 
Denis Akhiyarov
Sent: 20 June 2018 05:18
To: A list for users and developers of Python for .NET
Subject: -|EXT|- Re: [Python.NET] Cast PyObject to RoutedEventHandler

Have you tried @clrmethod decorator?

https://github.com/pythonnet/pythonnet/blob/master/src/runtime/resources/clr.py#L54
https://github.com/pythonnet/pythonnet/blob/master/src/tests/test_clrmethod.py#L14

On Tue, Jun 19, 2018 at 10:37 AM, WARD Steve 
<steve.w...@oxinst.com<mailto:steve.w...@oxinst.com>> wrote:
Can anyone tell me how to cast or Convert (or use a TryGet, or As whatever…), a 
PyObject (a Python function), to a C# RoutedEventHandler?

The Python function:

def OnStartButton(sender,args):
    #do stuff
    pass

Thanks
Steve
___________________________________________________________________________
This e-mail is confidential and is for the addressee only.   Please refer to
www.oxinst.com/email-statement<http://www.oxinst.com/email-statement> for 
regulatory information.

_________________________________________________
Python.NET mailing list - 
PythonDotNet@python.org<mailto:PythonDotNet@python.org>
https://mail.python.org/mailman/listinfo/pythondotnet

___________________________________________________________________________
This e-mail is confidential and is for the addressee only.   Please refer to
www.oxinst.com/email-statement<http://www.oxinst.com/email-statement> for 
regulatory information.
_________________________________________________
Python.NET mailing list - PythonDotNet@python.org
https://mail.python.org/mailman/listinfo/pythondotnet

Reply via email to