Porting from WPF to Silverlight: The Missing Pieces, Part 1

21 בספטמבר 2010

Recently I’ve been working on a port of a WPF 3.5 application into a Silverlight 3 web application. I came across many of the limitation of Silverlight 3 relative to WPF 3.5.


Fortunately, I’ve found numerous excellent solutions throughout the blogosphere.
These have helped me so much I would like to note them for all to see.
Also, this list might come in handy to other people porting applications from WPF to Silverlight.


One final note, the port was done from WPF 3.5 to Silverlight 3. Some of the issues might been solved in Silverlight 4 or have different solutions.


No Commands


Description: Silverlight 3 lacks proper support for commands.


Although Silverlight 3 does have the definition of ICommand, the buttons doesn’t have a Command property to bind with.


Solution by: Patrick Cauldwell


http://www.cauldwell.net/patrick/blog/MVVMBindingToCommandsInSilverlight.aspx 


In a nutshell:


Instead of writing:


 




<Button
    Content="Say Hello"
    Command="{Binding Path=SayHello}"
    />

 


Use the code in the post and write:




<Button
    Content="Say Hello"
    my:ButtonService.Command="{Binding Path=SayHello}"
    />

No ClipToBounds


Description: Silverlight 3 lacks support for ClipToBounds.


Solution by: Colin Eberhardt


http://www.scottlogic.co.uk/blog/colin/2009/05/silverlight-cliptobounds-can-i-clip-it-yes-you-can/


In a nutshell:


Instead of writing:


 




<Grid
    Background="Yellow"
    Margin="20,40,-20,20"
    ClipToBounds="True"
    >
    
</Grid>

 


Use the code in the post and write:


 




<Grid
    Background="Yellow"
    Margin="20,40,-20,20"
    util:Clip.ToBounds="True"
    >
    
</Grid>

 


No DataContextChanged Event


Description: Silverlight 3 doesn’t have the DataContextChanged event


Solution by: Emiel Jongerius


http://www.pochet.net/blog/2010/06/16/silverlight-datacontext-changed-event-and-trigger/


In a nutshell:


Instead of writing:




MyControl.DataContextChanged += MyControl_DataContextChanged;

Use the code in the post and write:


 




DataContextChangedHelper.AddDataContextChangedHandler(MyControl, MyControl_DataContextChanged);

 


No DataTrigger


Description: Silverlight 3 doesn’t have data triggers, it uses a completely different model, using VisualStateManager


Solution by: Pete Blois


http://blois.us/blog/2009/04/datatrigger-bindings-on-non.html


In a nutshell:


Instead of writing:




<DataTrigger Binding="{Binding IsLoaded}" Value="True">
    … DOING STUFF
<DataTrigger>

Use the code in the post and write:




<i:Interaction.Behaviors>
    <id:DataStateSwitchBehavior Binding='{Binding IsLoaded}'>
        <id:DataStateSwitchCase Value='True' State='Loaded'/>
    </id:DataStateSwitchBehavior>
</i:Interaction.Behaviors>

Of course, you should define a state that does the same “STUFF”.


The transition is not immediate, but at least it provide the same functionality as data triggers.


To be continued..


That’s it for now,
Arik Poznanski.


הוסף תגובה
facebook linkedin twitter email

כתיבת תגובה

האימייל לא יוצג באתר. (*) שדות חובה מסומנים

3 תגובות

  1. Anon21 בספטמבר 2010 ב 16:56

    - SL4 supports commanding.

    להגיב
  2. Adam Kinney24 בספטמבר 2010 ב 20:32

    Are you planning to write a post on why you are porting the application from WPF to Silverlight? And also, on why you've decided to only upgrade to Silverlight 3 and not Silverlight 4? That would be interesting.

    להגיב
  3. arik12 באוקטובר 2010 ב 20:05

    Adam,

    The reason for porting from WPF to SL was that we needed a Web version of the application.

    We chose Silverlight 3 because the next step was to port the application into Windows Phone 7, which currently support only Silverlight 3.

    All of this will be released as an open source project.

    להגיב