[Ironpython-users] problem incorporating namespace into Xaml

Jeff Hardy jdhardy at gmail.com
Fri Jul 25 10:25:57 CEST 2014

On Fri, Jul 25, 2014 at 12:45 AM, John Trinder <TrinderJohn at dsl.pipex.com>

>  I'm new to FePy and after doing a fair bit of studying am embarking on
> transcoding a C# project to FePy. I'm encountering a problem incorporating
> a module that defines a subclass of Canvas and then trying to utilize this
> custom canvas in the Xaml code.
> My skeleton files/modules and contents are as follows:
> *module1.py *import clr
> clr.AddReference('PresentationFramework')
> import System.Windows.Controls as swc
> class CustomCanvas(swc.Canvas):
>     def __init__(self):
>         pass
> *WpfApplication1.xaml*
> <Window
>        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
> <http://schemas.microsoft.com/winfx/2006/xaml/presentation>
>        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
> <http://schemas.microsoft.com/winfx/2006/xaml>
>        *xmlns:local="clr-namespace:module1"*
>        Title="WpfApplication1" Height="300" Width="300">
>        <Grid>
>             *<local:CustomCanvas HorizontalAlignment="Left" Height="100"
> Margin="96,90,0,0" VerticalAlignment="Top" Width="100"/>*
>        </Grid>
> </Window>
> *WpfApplication1.py*
> import wpf
> from System.Windows import Application, Window
> class MyWindow(Window):
>     def __init__(self):
>         wpf.LoadComponent(self, 'WpfApplication1.xaml')
> Application().Run(MyWindow())
> I've marked in bold where I have incorporated my module/namespace.
> Intellisense says that on the *<local:CustomCanvas .../>* line,
> CustomCanvas 'does not exist in the namespace *"clr-namespace:module1"'*
> I'm guessing that it's not possible to do (yet) what I want to do - which
> is pretty basic.
> Thanks in advance btw for any advice and tips.

I'm no WPF expert, but I'm guessing the issue is that IronPython doesn't
statically build a "real" .NET class, and the namespace and class name
aren't guaranteed to be the same on each run.*

It is possible to manipulate the type generation using __clrtype__[1] but
it's not well documented. That allows you to control the exact type name
that is used so that other code can look it up using reflection.

I have grand plans for how this will in IronPython, where classes can take
keyword args and there are function attributes to set types:

  class CustomCanvas(swc.Canvas, namespace="Foo"):

Then you could use *xmlns:local="clr-namespace:Foo" *and it would work as
expected. That's still a ways out, though - I'd have to implement support
for those keyword args first. :)

- Jeff

[1] http://ironpython.net/documentation/dotnet/dotnet.html#clrtype

* In your case, the backing type name would be something like
"IronPython.Types.System.Windows.Controls.Canvas$1" - and it's the "$1"
that can change on each run.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ironpython-users/attachments/20140725/e6637a5e/attachment.html>

More information about the Ironpython-users mailing list