[IronPython] IronPython/Silverlight -- callback problem

Jan-Philip Gehrcke jgehrcke at googlemail.com
Thu Jul 8 13:28:51 CEST 2010


  Thanks for your comment, Michael.
I found the explanation in the docs: 
http://msdn.microsoft.com/en-us/library/ms619201(v=VS.95).aspx
The default setting is `BitmapCreateOptions.DelayCreation`
-> "Causes a BitmapSource object to delay initialization until it is 
necessary. This is useful when dealing with collections of images. This 
is the default value of the BitmapImage.CreateOptions property in 
Silverlight."

Now, I change this to `BitmapCreateOptions.None` and add the callbacks 
before assigning the URI:

     def load_and_show(self):
         self.bi = BitmapImage()
         self.bi.CreateOptions = BitmapCreateOptions.None;
         self.bi.ImageOpened += self.successfully_loaded_now_show
         self.bi.ImageFailed += self.load_failed
         self.bi.UriSource = Uri(self.abspath, UriKind.Absolute)

     def successfully_loaded_now_show(self, s, e):
         self.xaml_main_image.Source = self.bi

This works as expected.

Jan-Philip

On 07.07.2010 18:49, Michael Foord wrote:
> On 06/07/2010 15:59, Jan-Philip Gehrcke wrote:
>> Hello,
>>
>> I don't know if this is more an IronPython (2.6.1) issue than a 
>> Silverlight (4) issue, but I hope that I am talking to the right 
>> audience again (thank you for the great answers last time).
>>
>> I've created an `Image` class managing images based on 
>> System.Windows.Media.Imaging.BitmapImage. My method 
>> `image_instance.load_and_show()` should load the image from a given 
>> URI and then show it in an XAML <image /> element. The important 
>> thing is that the showing part should only happen after it is sure 
>> that the image has loaded correctly. To control this, I use the 
>> BitmapImage.ImageOpened event. This is the relevant part of the code 
>> from my `Image` class:
>>
>>
>> def load_and_show(self):
>> self.bi = BitmapImage(Uri(self.abspath, UriKind.Absolute))
>> self.bi.ImageOpened += self.successfully_loaded_now_show
>> self.bi.ImageFailed += self.load_failed
>> #self.xaml_main_image.Source = self.bi # LINE A
>>
>> def successfully_loaded_now_show(self, s, e):
>> debug("image loaded: %s" % s.UriSource) # LINE B
>> self.xaml_main_image.Source = self.bi # LINE C
>>
>> def load_failed(self, s, e):
>> debug("image load failed") # LINE D
>>
>>
>> If the code is like shown above and I do 
>> `image_instance.show_and_load()`, nothing happens. More exactly, LINE 
>> B, C, and D are not reached. It is like *neither the event 
>> ImageOpened nor ImageFailed is toggled*.
>>
>
> It seems likely then that the image is not opened or downloaded until 
> you set it as the source on an image. Does the code behave differently 
> if you write it in C#?
>
> If you set the bitmapimage as the image source does the event then 
> fire correctly? If so perhaps create the image control but only add it 
> to the visual tree once the bitmap has loaded.
>
> Michael
>
>> If I re-activate (or un-outcomment =)) LINE A, the callbacks 
>> magically work properly, i.e. after calling 
>> `image_instance.show_and_load()` the image shows up in the UI (either 
>> via LINE A or C), and LINE B is definitely reached (or LINE D in case 
>> of a wrong URI).
>>
>> The point is that I don't want LINE A, because showing the image 
>> should happen in a more controlled manner. What's the problem in my 
>> approach, why are the callbacks not called?
>>
>> Thank you in advance,
>>
>> Jan-Philip Gehrcke
>> -- 
>> http://gehrcke.de
>> _______________________________________________
>> Users mailing list
>> Users at lists.ironpython.com
>> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
>
>




More information about the Ironpython-users mailing list