[IronPython] Loading Images from XAML with XamlReader (WPF)

Michael Foord fuzzyman at voidspace.org.uk
Thu Dec 13 15:37:26 CET 2007


Hello guys,

I'm struggling to load images from XAML using the XamlReader with 
IronPython. The equivalent with compiled XAML and C# works fine - 
however this doesn't directly use the XamlReader so I wonder if it is an 
issue with that?

The Python code is:

import clr
clr.AddReference("PresentationFramework")
clr.AddReference("PresentationCore")

from System.IO import File
from System.Windows.Markup import XamlReader

from System.Windows import (
   Application, Window
)

class HelloWorld(Window):
   def __init__(self):
      stream = File.OpenRead("HelloWorld.xaml")
      self.Content = XamlReader.Load(stream)
     
hello = HelloWorld()

app = Application()
app.Run(hello)


The XAML is:

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <Grid>
        <Image Source="images/image.jpg" Stretch="None" 
HorizontalAlignment="Center" VerticalAlignment="Center"/>
    </Grid>

</Window>

The image stubbornly refuses to appear and reports an ActualHeight and 
ActualWidth of 0.0 if I inspect it.

I've even tried instantiating a BitmapImage directly from code and 
setting it as the Source on the image:

      image = root.FindName("image")

      bi = BitmapImage()
      bi.BeginInit()
      bi.UriSource = Uri("/images/image.jpg", UriKind.RelativeOrAbsolute);
      bi.EndInit()
      image.Source = bi

Same problem - the image doesn't show. Anyone got any clues?

Michael Foord
http://www.voidspace.org.uk/python/weblog/index.shtml



More information about the Ironpython-users mailing list