Showing posts with label WPF Vs Silverlight. Show all posts
Showing posts with label WPF Vs Silverlight. Show all posts

Friday, 9 December 2011

When Should I use WPF Vs Silverlight

In my role, I work with a number of large corporations, and this is a question that they ask me regularly: What is the difference between the Windows Presentation Foundation (WPF) and Silverlight, and in what scenarios does it make sense to use each?
Microsoft feels that user experience is important, and invested in multiple technologies to promote better user experience. Both WPF and Silverlight use XAML (Extensible Application Markup Language) under the covers.
Let's look at some of the different characteristics of each technology:

WPF:
  • Ships as part of the .NET Framework (version 3.0 and onward)
  • Runs as Windows application or as web "browser application" (called XBAP, for "XAML Browser Application"). Note that XBAPs run only in Internet Explorer with .NET 3.0 and in both Internet Explorer and Firefox with .NET 3.5.
  • Runs on Windows machines only (Windows XP, Windows Server 2003, Windows Vista, and Windows Server 2008)
  • Richest functionality, including 3D graphics
Silverlight:
  • Ships independently
  • Runs in web browsers only (Internet Explorer, Firefox, Safari)
  • Runs on Windows or Mac operating systems (also on Linux via Moonlight, which is an open source implementation of Silverlight based on Mono)
  • Functionality is a subset of WPF's feature set

When should you use each? The maddening answer is (of course): it depends!
WPF is a more mature technology and was designed with a richer feature set. It also has the advantage of being able to run in a browser or as an installed Windows-Form-type app.
Silverlight has a broader reach. You can access Silverlight from many operating systems and web browsers.
The most important reason to choose one over the other should be based on the intended audience for the application. For example, if a corporation is designing an application for internal use only and every employee has Windows XP as the company standard OS, then go with WPF to leverage the richer feature set. If a corporation is designing an external-facing website, then Silverlight is the better choice because potential customers can access the website from a variety of different operating systems and browsers.

Wednesday, 12 January 2011

Converter Parameter with bound variable

You will run into a situation where you need to convert a value based on another value in the object. As you would know by now, you can not use {Binding variable} in convert parameter. If you do, even though you will not any compilation error, you will get run time error. So how do we do that. You can pass static variable in the convert parameter.

So a simple work around for this problem would be to bind as following in your XAML.

 <c1:Column Header="Amount" Binding="{Binding Converter={StaticResource myConverter}, ConverterParameter=Amount}" GroupAggregate="Sum" Width="100"/>

In the above XAML instead of binding it to a object variable, I bound it to the row and then call my converter passing the string name of the column I am interested in.

   1:   public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
   2:   {
   3:          Person p = value as Person;
   4:          if (p.Amount < 0 && p.Name.Equals("Kiran"))
   5:             return p.Amount + p.Amount * p.Bonus;
   6:          else
   7:             return p.Amount;
   8:          return value;
   9:   }

In the above code as you can see, first thing I do, is to unbox the object to the class and in here it is ‘Person’ class. Once you do that, from there you have access to all the properties in the class for you in the Convert method.  In the above code, as you can see, if the name is “Kiran” then my salary would salary + bonus other wise just salary.