<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="Generator" content="Microsoft Word 14 (filtered medium)">
<style><!--
/* Font Definitions */
@font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
        {font-family:Tahoma;
        panose-1:2 11 6 4 3 5 4 4 2 4;}
@font-face
        {font-family:Consolas;
        panose-1:2 11 6 9 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0cm;
        margin-bottom:.0001pt;
        font-size:12.0pt;
        font-family:"Times New Roman","serif";}
a:link, span.MsoHyperlink
        {mso-style-priority:99;
        color:blue;
        text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
        {mso-style-priority:99;
        color:purple;
        text-decoration:underline;}
span.EmailStyle17
        {mso-style-type:personal-reply;
        font-family:"Calibri","sans-serif";
        color:#1F497D;}
.MsoChpDefault
        {mso-style-type:export-only;
        font-family:"Calibri","sans-serif";
        mso-fareast-language:EN-US;}
@page WordSection1
        {size:612.0pt 792.0pt;
        margin:72.0pt 72.0pt 72.0pt 72.0pt;}
div.WordSection1
        {page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]-->
</head>
<body lang="EN-GB" link="blue" vlink="purple">
<div class="WordSection1">
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D">I tried:<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D"><o:p> </o:p></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:9.5pt;font-family:Consolas;color:blue;mso-fareast-language:EN-US">import</span><span style="font-size:9.5pt;font-family:Consolas;color:black;mso-fareast-language:EN-US">
</span><span style="font-size:9.5pt;font-family:Consolas;color:#6F008A;mso-fareast-language:EN-US">System</span><span style="font-size:9.5pt;font-family:Consolas;color:black;mso-fareast-language:EN-US"><o:p></o:p></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:9.5pt;font-family:Consolas;color:blue;mso-fareast-language:EN-US">import</span><span style="font-size:9.5pt;font-family:Consolas;color:black;mso-fareast-language:EN-US">
</span><span style="font-size:9.5pt;font-family:Consolas;color:#6F008A;mso-fareast-language:EN-US">System</span><span style="font-size:9.5pt;font-family:Consolas;color:black;mso-fareast-language:EN-US">.</span><span style="font-size:9.5pt;font-family:Consolas;color:#6F008A;mso-fareast-language:EN-US">Windows</span><span style="font-size:9.5pt;font-family:Consolas;color:black;mso-fareast-language:EN-US"><o:p></o:p></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:9.5pt;font-family:Consolas;color:black;mso-fareast-language:EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-size:9.5pt;font-family:Consolas;color:black;mso-fareast-language:EN-US">@</span><span style="font-size:9.5pt;font-family:Consolas;color:#6F008A;mso-fareast-language:EN-US">clr</span><span style="font-size:9.5pt;font-family:Consolas;color:black;mso-fareast-language:EN-US">.clrmethod([</span><span style="font-size:9.5pt;font-family:Consolas;color:#6F008A;mso-fareast-language:EN-US">System</span><span style="font-size:9.5pt;font-family:Consolas;color:black;mso-fareast-language:EN-US">.object,
</span><span style="font-size:9.5pt;font-family:Consolas;color:#6F008A;mso-fareast-language:EN-US">System</span><span style="font-size:9.5pt;font-family:Consolas;color:black;mso-fareast-language:EN-US">.Windows.RoutedEventArgs])<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:9.5pt;font-family:Consolas;color:blue">def</span><span style="font-size:9.5pt;font-family:Consolas;color:black"> OnStartButton(</span><span style="font-size:9.5pt;font-family:Consolas;color:gray">sender</span><span style="font-size:9.5pt;font-family:Consolas;color:black">,</span><span style="font-size:9.5pt;font-family:Consolas;color:gray">args</span><span style="font-size:9.5pt;font-family:Consolas;color:black">):<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:9.5pt;font-family:Consolas;color:black">   
</span><span style="font-size:9.5pt;font-family:Consolas;color:green">#do stuff<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:9.5pt;font-family:Consolas;color:black">   
</span><span style="font-size:9.5pt;font-family:Consolas;color:blue">pass</span> <o:p>
</o:p></p>
<p class="MsoNormal"><span style="font-size:9.5pt;font-family:Consolas;color:black;mso-fareast-language:EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D">and get: ‘</span><span style="font-size:11.0pt;font-family:"Calibri","sans-serif"">Cannot perform runtime binding on a null referenceAttributeError : object’<span style="color:#1F497D"><o:p></o:p></span></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D">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#!!<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D">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:<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D"><o:p> </o:p></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:9.5pt;font-family:Consolas;color:green;mso-fareast-language:EN-US"># works in IronPython</span><span style="font-size:9.5pt;font-family:Consolas;color:black;mso-fareast-language:EN-US">
<o:p></o:p></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:9.5pt;font-family:Consolas;color:black;mso-fareast-language:EN-US">b =
</span><span style="font-size:9.5pt;font-family:Consolas;color:gray;mso-fareast-language:EN-US">app</span><span style="font-size:9.5pt;font-family:Consolas;color:black;mso-fareast-language:EN-US">.Button(</span><span style="font-size:9.5pt;font-family:Consolas;color:#A31515;mso-fareast-language:EN-US">"Start",
</span><span style="font-size:9.5pt;font-family:Consolas;color:black;mso-fareast-language:EN-US">OnStartButton)<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:9.5pt;font-family:Consolas;color:black;mso-fareast-language:EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-size:9.5pt;font-family:Consolas;color:green;mso-fareast-language:EN-US"># can’t use OnStartButton as argument, have to add this in Python.NET :-(<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:9.5pt;font-family:Consolas;color:black;mso-fareast-language:EN-US">b.Click += OnStartButton</span><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D"><o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D">By the way I’m using Python.Net with PyScope from my own build.<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D">Steve<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D"><o:p> </o:p></span></p>
<p class="MsoNormal"><b><span lang="EN-US" style="font-size:10.0pt;font-family:"Tahoma","sans-serif"">From:</span></b><span lang="EN-US" style="font-size:10.0pt;font-family:"Tahoma","sans-serif""> PythonDotNet [mailto:pythondotnet-bounces+steve.ward=oxinst.com@python.org]
<b>On Behalf Of </b>Denis Akhiyarov<br>
<b>Sent:</b> 20 June 2018 05:18<br>
<b>To:</b> A list for users and developers of Python for .NET<br>
<b>Subject:</b> -|EXT|- Re: [Python.NET] Cast PyObject to RoutedEventHandler<o:p></o:p></span></p>
<p class="MsoNormal"><o:p> </o:p></p>
<div>
<p class="MsoNormal">Have you tried @clrmethod decorator?<o:p></o:p></p>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal"><a href="https://github.com/pythonnet/pythonnet/blob/master/src/runtime/resources/clr.py#L54">https://github.com/pythonnet/pythonnet/blob/master/src/runtime/resources/clr.py#L54</a><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><a href="https://github.com/pythonnet/pythonnet/blob/master/src/tests/test_clrmethod.py#L14">https://github.com/pythonnet/pythonnet/blob/master/src/tests/test_clrmethod.py#L14</a><o:p></o:p></p>
</div>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
<div>
<p class="MsoNormal">On Tue, Jun 19, 2018 at 10:37 AM, WARD Steve <<a href="mailto:Steve.Ward@oxinst.com" target="_blank">Steve.Ward@oxinst.com</a>> wrote:<o:p></o:p></p>
<div>
<div>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto">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?<o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"> <o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto">The Python function:<o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"> <o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;text-autospace:none">
<span style="font-size:9.5pt;font-family:Consolas;color:blue">def</span><span style="font-size:9.5pt;font-family:Consolas;color:black"> OnStartButton(</span><span style="font-size:9.5pt;font-family:Consolas;color:gray">sender</span><span style="font-size:9.5pt;font-family:Consolas;color:black">,</span><span style="font-size:9.5pt;font-family:Consolas;color:gray">args</span><span style="font-size:9.5pt;font-family:Consolas;color:black">):</span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;text-autospace:none">
<span style="font-size:9.5pt;font-family:Consolas;color:black">    </span><span style="font-size:9.5pt;font-family:Consolas;color:green">#do stuff</span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="font-size:9.5pt;font-family:Consolas;color:black">   
</span><span style="font-size:9.5pt;font-family:Consolas;color:blue">pass</span> <o:p>
</o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"> <o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto">Thanks<o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto">Steve<o:p></o:p></p>
</div>
<p class="MsoNormal">___________________________________________________________________________<br>
This e-mail is confidential and is for the addressee only.   Please refer to <br>
<a href="http://www.oxinst.com/email-statement" target="_blank">www.oxinst.com/email-statement</a> for regulatory information.
<o:p></o:p></p>
</div>
<p class="MsoNormal" style="margin-bottom:12.0pt"><br>
_________________________________________________<br>
Python.NET mailing list - <a href="mailto:PythonDotNet@python.org">PythonDotNet@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/pythondotnet" target="_blank">https://mail.python.org/mailman/listinfo/pythondotnet</a><o:p></o:p></p>
</div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
</div>
___________________________________________________________________________<br>
This e-mail is confidential and is for the addressee only.   Please refer to <br>
<a href="http://www.oxinst.com/email-statement">www.oxinst.com/email-statement</a> for regulatory information.
</body>
</html>