Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
  • Why switch to WPF?

    • 0
    • 2
    • 1
    • 1
    • 0
    • 0
    • 0
    • 0
    • 1.33k
    Comment on it

    WinForms still exists with WPF in Visual Studio. It is still in use but getting outdated as software user interfaces are getting more demanding in terms of speed and slick animations In this article It is mentioned that WPF is not created to replace Windows forms. It may be possible but WPF is taking over WinForms real fast.

    When WPF was launched with it's shiny UI demos ,it was looked as eye candy but still the workhorse was WinForms. But no technology is perfect when it's launched and improvements are required time to time. Every technology required improvements in it's subsequent releases and so did WPF. But now that's not the case. Currently WPF is the choice for very large projects. If you need some examples,take a look at your Yahoo Messenger and Visual Studio(2010 onwards).

    So what would be the possible reasons to switch to WPF from WinForms. We can define those reasons as :-
    A - Anywhere execution ( Windows or Web)

    B - Bindings ( less coding)

    C - Common look and feel ( resource and styles)

    D - Declarative programming (XAML)

    E - Expression blend animation ( Animation ease)

    F - Fast execution ( Hardware acceleration)

    G - Graphic hardware independent ( resolution independent)

    So this "ABCDEFG" are the advantages of WPF over WinForms. This is not the full and final list of reasons but this is certainly a list of reasons which can make you think about switching to WPF. Let's get into details of each of the reasons

    A - Anywhere Execution ( Windows or Web)
            Create one WPF application and you can run it as a Desktop or a Browser(XBAPs-XAML Browser Applications) application. You can create a WPF Browser Application(Project Template in Visual Studio Project) which uses .Net Framework and call in a browser like:-
    http://localhost/WpfBrowserApplication1.xbap .The .Net framework is required on the desktop as this is the environment required for any .Net application.

    B - Bindings ( less coding)
           Bindings are most powerful feature in WPF. In WPF any of the control can be bound to anything like CLR properties,Dependency properties( Advanced topic in WPF so we'll talk about them later),or to a control property,hence increasing the responsiveness of the UI. Didn't get it? Just create a WPF application and copy following code inside window element of a page or form(both are different but we can assume them as similar for now):-

       <StackPanel Margin="10">
                        <TextBox Name="txtValue" />
                        <WrapPanel Margin="0,10">
                                <TextBlock Text="Value: " FontWeight="Bold" />
                                <TextBlock Text="{Binding Path=Text, ElementName=txtValue}" />

    Now when you run the project you can see that the TextBlock(can be assumed as label of Winforms) is populated instantaneously as we type in TextBox. Following is the magic trick that is making it possible:-

     Binding Path=Text, ElementName=txtValue

    Hope you like it over Winforms as the same task would take a lot of C# coding for that. So what is the problem with c# code for UI things? Explained in the next point:-)

    C - Common look and feel ( resource and styles)
                   What is the general approach of styling a form in WinForms. The answer is-just define the style in the form. Sounds nice and easy,right? Now Imagine you have over 100 forms,then scenario becomes scarier. So immediately you'd be thinking about having a concept like CSS so that you could define it once and get a common look and feel across the application. But that is not available in WinForms. Luckily the CSS like concept is available in WPF. You can create resources which might be Application wide style as an Application Resource and provide reference of it in your controls. You can create the style and use it in following way. Of course you can't get the code part but try getting the concept shown in the pic. For Example-

    alt text
    Fig 1-Creating and using Styles in WPF
    D - Declarative programming (XAML)
                  "Yeah.That's the thing which caught my eyes since I first created this WPF application",you will say. What is that? Looks like language in .aspx page to me.
    Then I'll say that It's a new markup language and It's based on XML. And it is called XAML(Extensible Application Markup Language).
    A new language? Ok,Now we need to learn a new language for it. The answer is-Yes.
    You'll say-Ok. Then what is the purpose of this new thing and tell me the problem with c#.
    Two Major reasons here.
    1-C# is compiled and XAML is parsed with XAML Parser. Compilation is a time consuming process but on the other hand parsing his like translating into actual meaning by using a Parser. So the UI declared with XAML is faster than UI declared with c#.

    2-Lesser lines of code in code behind. Compare following lines of code for the same tasks :-
    Declaring a button
    In XAML

    <Grid x:Name="ContentPanel" Margin="12,0,12,0">
        <Button Height="72" Width="160" Content="Click Me" />

    In C#

    // Initialize the button
            Button myButton = new Button();
            // Set its properties
            myButton.Width = 160;
            myButton.Height = 72;
            myButton.Content = "Click Me";
            // Attach it to the visual tree, specifically as a child of
            // a Grid object (named 'ContentPanel') . In other words, position the button in the Grid.

    So this code example shows that what we are achieving by using this declarative programming style.

    E - Expression blend animation ( Animation ease)
           When it comes to design,you are not left only with visual studio. Microsoft has provided you with a tool named Expression Blend for easy design of UI and in VS 2013 it comes as default installation. The default nature of a WPF application allows you to maintain a clear separation of concerns so your designer can work on his designs separately from the developer on Expression Blend.
    Blend supports animation extensively to build a fluid and responsive UI with the use of animation. It supports a very complex level of animation. If you are interested to see an example,click here and go through the tutorial

    F - Fast execution ( Hardware acceleration)
           For this point we need to know about two crucial parts of our computer.
    CPU and GPU.

    Well,we know the CPU but what the hell is the GPU. GPU is your Graphics Processing Unit and whether you like it or not it is present in your PC as discreet or motherboard embedded form. It is responsible for processing the Graphics in your PC like your wallpapers,movies,games and UI in your application etc.
    So how this GPU is helpful in faster execution of WPF application?

    Almost every application created for Windows(not DOS) have their User Interface. So If we talk about winforms here, it uses GDI(Graphics Device Interface) which was good in Windows XP era applications when animated UI was not in very fashion and hence it relies on a library which had a logic of converting shapes to desired format and that used a lot of CPU(Why using CPU for Graphics when GPU is there?).

    In order to obtain the GPU-accelerated rendering, Windows Presentation Foundation (WPF) renders and presents graphics through the DirectX in place of GDI which is slower for fast animations for today's world.
    A question can be asked here. If my PC is not having any discreet Graphics Card, and I am using a WPF application which has a heavily animated UI,will my PC respond slower?
    The answer to this question is both yes and no. Because that depends upon how the developer has handled the system check for handling animations. Yes ,WPF does this with a three-tiered fashion:

    1-If your video card does not provide hardware acceleration, WPF does the software acceleration or goes to Tier 0 rendering mode(Below Direct X 7.0).

    2-If your video card supports partial hardware acceleration(For Example-Graphics Chipset Embeded in Motherboard), then WPF renders in Tier 1 rendering mode.(DirectX 7.0 to DirectX 9.0)

    3-If Your video card supports full hardware acceleration(Dedicated Graphics Card),WPF renders in Tier 3 mode.(above DirectX 9.0)

    Need a real world example in WPF? Take a look at your OS(Windows Vista or Higher than Windows Vista because this is the first OS in which WPF was implemented at a large scale). There is a thing called Aero Theme or Aero Peek. It is enabled only when you have a graphics card matching to the requirement of the operating system.

    G - Graphic hardware independent ( resolution independent)
            In windows forms the measurement unit was in pixels. It was good when monitors where use 96 pixels per inch measurement, but problem started when a variety of display units with different resolutions and size came into market. For example -720*1280(720p) resolution can be found on a 5 inch mobile as well as on a 19 inch monitor.

    So with Winforms, a button 100 pixel width will appear 1 inch wide on a 96 pixel/inch monitor but if we see it on a 12 pixel/inch monitor(higher resolution) it'll appear to be 4/5th of an inch(smaller). Button will be changing it's perspective size(not actual) for the user,which is a problem if we talk in user interface consistency terms.

    In WPF Microsoft used DIU(Device Independent Units) or DIP(P=Pixel here).It's measurement is absed on inches rather than pixels .A DIU is smaller than a point(point is defined as 1/72 of an inch but DIU is defined as 1/96 of inch).

    So 1DIP=1/96 inch(1 DIP=1pixel at 96 DPI) on 120DPI=120/96=1.25 pixels(approx)

    So when the measurement doesn't comes as a perfect round number,WPF uses anti-aliasing to snap out fuzzy pixels and hence creating smoother borders and lines. In the example we can say that the pixels will not change to 2 until you set the DPI to 182(zooming your screen to 190% !!)

    If the anti-aliasing is proving too much for you to understand,you can just get anti-aliasing as removing rough edges of a window or a control when enlarging it. By the way you can always search the term and it's not too tough to understand.

    So these are ABCDEFG of WPF. Of course there may be IJKL...and so on but this ABC.. of WPF can be taken as core factors of opting WPF as a development technology for developing Desktop solutions. Please do ask the questions which you have in the comments section.

    Happy Coding:-)

 0 Comment(s)

Sign In

Sign up using

Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: