Tag Archives: WPF

Expression Blend Samples not working with Silverlight 4 and WPF 4

This blog post is just to document my experience with the Expression Blend Sample source together with Silverlight 4 or WPF 4.0

If you are using the Expression Blend Samples from codeplex (http://expressionblend.codeplex.com) then you might experience some problems when using the included triggers and behavior within SL4 or WPF4 runtime. The problem is that a dependency property with the type System.Windows.Data.Binding is not set to due to a change in the MS.Internal.Data.DynamicValueConverter class. Since SL4 / WPF4 the DynamicValueConverter uses the value of the binding instead of the binding instance itself. As example, the Command property of the InvokeDataCommand trigger is declared as System.Windows.Data.Binding, but the DynamicValueConverter will return the value of the binding, an ICommand compatible instance, and not the binding instance itself. So, the command property will always be null, because DynamicValueConverter cannot cast the object. And if the command binding cannot be set, the BindingListener, which is used internally, will not work correctly.

image

If we change the binding target from a dependency property to a simple CLR property, then the DynamicValueConverter can set the property and InvokeDataCommand works as before.

image

I’ve found that codeplex user AvdMeulen changed the whole Expression sample source and submitted a zip package on codeplex. See link below.

 

Let’s go into more details:

Silverlight 3 does not allow using data binding on types which are NOT inherited from class FrameworkElement. That means that the entire Silverlight standard behaviors and triggers do not support data binding, because the Trigger and Behavior classes are directly derived from DepedencyObject. Nevertheless, if you use data-binding with a trigger- or behavior property in Silverlight 3, then the binding system does not execute the binding, but it tries to set the binding instance itself to the property as a value of type System.Windows.Data.Binding. This will be done through the MS.Internal.Data.DynamicValueConverter class. If the target property is not of type System.Windows.Data.Binding it will not work and you get an AG_E_PARSER_BAD_PROPERTY_VALUE exception from the XAML parser. Based on this binding-engine behavior, Pete Blois invented the BindingListener class.
His trick was to declare properties on behaviors and triggers as type of System.Windows.Data.Binding in order to get them bindable. The binding is still not executed by the binding system and to bind them he declared a dummy attached dependency property and set the binding instance to the attached dependency property. The result is that the attached dependency property can execute the binding and we can just listen to when the attached dependency property changes. This results in a full functional binding for non-FrameworkElements. All this is encapsulated in the BindingListener class.

But now, why it does not work for SL4 anymore? The answer can be found in the breaking changes for Silverlight 4.

image

 

With SL4 it’s now possible to data binding dependency properties on triggers and behavior too, which makes the BindingListener class useless. This is also the reason why the Binding property remains null, because the SL4 binding engine set a BindingExpression and not the Binding itselfs and the BindingExpression tries to set the Command value instead of the Binding instance.

 

Link resources:
http://expressionblend.codeplex.com/workitem/8148
http://www.sztronka.com/2010/01/behaviors-triggers-and-memory-leaks-in.html
http://mtaulty.com/CommunityServer/blogs/mike_taultys_blog/archive/2010/03/27/silverlight-4-blend-4-mvvm-binding-dependencyobject.aspx
http://compiledexperience.com/blog/posts/blendable-mvvm-commands-and-behaviors
http://blois.us/blog/2009/04/datatrigger-bindings-on-non.html

Advertisements
Tagged ,

Deep Zoom Collection Downloader

Just for fun I’ve implemented a Deep Zoom Tiles Downloader which recreates the original image from a deep zoom composition. Additionally the downloader can download the original composition from the website to your local disk. The downloader follows the principals of a pure MVVM application.

No, i have not too much time. I’m pretty well experienced with the MVVM pattern in Silverlight but not in the WPF runtime. To get more familiar with MVVM in WPF and to see the difference in a practical manner I decided to put the implementation into a ViewModel and to build a WPF-View on top of them.

The Application

In the Image Rebuilder tab, the application allows to set the deep zoom image settings or discover them automatically from a standard Deep Zoom Composer xml-file.

image

The downloader allows you to set each deep zoom composition parameter manually, because there are deep zoom compositions out there in the internet without having the standard Deep Zoom Composer XML format.

One nice example of such a collection is the “Matterhorn Deep Zoom Collection” created by Ronnie Saurenmann (Microsoft Switzerland).

image

Enter the following values to test the downloader with his collection:

 

In the Tiles Downloader tab, the application allows to set the root folder of the deep zoom composition (containing the folder levels 0, 1, 2, 3, 4, 5, etc), specifying the download target folder and to start the download.

image

 

ViewModel implementation

I’m using the Bitmap from the GDI to rebuild the image. A 32 bit RGB formatted bitmap uses 32 bit i.e. 4 bytes to store data for each pixel. So if you try to rebuild a large deep zoom composition with as example of 50000×50000 pixels then the memory the downloader needs to allocate dynamically for the image is very large 50000*50000*4 bytes which is roughly about 9 GB in memory. While this is not possible with GDI the downloader downloads the image in smaller portion so you might get an image portioned as following (example show image in 5 portions):

image

It should be very easy to build the final image with Photoshop or ImageMagick from the portioned tiles. Feel free to extend the downloader with a better approach while using a 3rd party image lib.

A note about the performance: the downloader rebuilds or downloads images tile-by-tile. I did not have enough time to do it parallel in several threads.

Summary

It’s quite the same to implement a ViewModel in WPF and Silverlight for such very small applications. The only difference I can see so far is the missing constraint for asynchron web request in WPF. This makes the implementation a little bit simpler. The binding expressions are identically to Silverlight, because I don’t need any special here. The command binding was easier to write because the Command property is supported from the ButtonBase natively (Silverlight will provide the same in version 4).

I know, this is a quite simple application and things get complicated when the application gets more complex.

Feedbacks are welcome.

Source code

You can download the source code here:

http://files.thekieners.com/blogcontent/2010/DeepZoomDownloader/source.zip

One final note

This downloader was implemented just for fun. Please consider copyrights when downloading deep zoom images.

Tagged , , ,

My first Microsoft Certification Exam

Last week I have passed the Microsoft Certification Exam 70-502 “Windows Presentation Foundation Application Development” successfully. After working about 2 year with WPF/Silverlight technology I decided to verify my knowledge with this exam.

Here my result:

image

 

I used the book “MCTS Self-Paced Training Kit as my main study guide.
My final preparation duration with this book was around one month.

image

Next step is to pass the Exam 70-536 “Microsoft .NET Framework Application Development”.
Let’s see. Maybe next month.

Tagged , , , ,