TOC

This article is currently in the process of being translated into Greek (~45% done).

About WPF:

WPF vs. WinForms

Στην προηγούμενη ενότητα, μιλήσαμε περί του τι είναι το WPF και λίγο για το WinForms. Σε αυτή την ενότητα, θα προσπαθήσουμε να τα συγκρίνουμε, επειδή, ενώ και τα δύο έχουν τον ίδιο σκοπό, διαφέρουν σε πολλά σημεία. Αν δεν έχετε δουλέψει ποτέ με WinForms στο παρελθόν, και ειδικά αν το WPF είναι το πρώτο σας GUI framework, μπορείτε να παραλείψετε αυτήν την ενότητα. Αν, όμως, σας ενδιαφέρουν οι διαφορές τους, συνεχίστε την ανάγνωση.

Η πιο σημαντική διαφορά μεταξύ WinForms και WPF είναι το γεγονός ότι ενώ το WinForms είναι απλώς ένα layer πάνω από τα βασικά Windows controls (π.χ. Πεδίο κειμένου), το WPF έχει φτιαχτεί από την αρχή και, σχεδόν σε όλες τις περιπτώσεις, δεν βασίζεται σε βασικά Windows controls. Μπορεί να φαίνεται μια μικρή διαφορά, αλλά δεν είναι, κάτι που θα γίνει ιδιαίτερα αντιληπτό αν έχετε ποτέ εργαστεί με κάποιο framework που βασίζεται σε Win32/WinAPI.

Ένα εξαιρετικό παράδειγμα αυτού είναι ένα κουμπί με μία εικόνα και ένα κείμενο πάνω του. Αυτό δεν είναι ένα τυπικό control των Windows, επομένως το WinForms δεν σας παρέχει άμεσα τη δυνατότητα. Αντιθέτως θα πρέπει να σχεδιάσετε την εικόνα μόνοι σας, να υλοποιήσετε το δικό σας κουμπί το οποίο θα υποστηρίζει εικόνες ή να χρησιμοποιήσετε ένα έτοιμο control κάποιου τρίτου. Με το WPF, ένα κουμπί μπορεί να περιέχει οτιδήποτε επειδή στην πραγματικότητα είναι ένα περίγραμμα με περιεχόμενο και διάφορες καταστάσεις (π.χ. untouched, hovered, pressed). Το κουμπί του WPF, όπως και τα περισσότερα controls του WPF, μπορεί να περιέχει μία μεγάλη γκάμα από controls εντός του. Επιθυμείτε ένα κουμπί με μία εικόνα και κείμενο; Απλώς τοποθετήστε ένα Image και ένα TextBlock control μέσα στο κουμπί και είστε έτοιμοι! Αυτή η ευελιξία δεν παρέχεται από τα controls του WinForms, κάτι το οποίο είναι ο λόγος που υπάρχει μία μεγάλη αγορά από απλές υλοποιήσεις controls όπως κουμπιά με εικόνες και ούτω καθεξής.

The drawback to this flexibility is that sometimes you will have to work harder to achieve something that was very easy with WinForms, because it was created for just the scenario you need it for. At least that's how it feels in the beginning, where you find yourself creating templates to make a ListView with an image and some nicely aligned text, something that the WinForms ListViewItem does in a single line of code.

This was just one difference, but as you work with WPF, you will realize that it is in fact the underlying reason for many of the other differences - WPF is simply just doing things in its own way, for better and for worse. You're no longer constrained to doing things the Windows way, but to get this kind of flexibility, you pay with a little more work when you're really just looking to do things the Windows way.

The following is a completely subjective list of the key advantages for WPF and WinForms. It should give you a better idea of what you're going into.

WPF advantages

  • It's newer and thereby more in tune with current standards
  • Microsoft is using it for a lot of new applications, e.g. Visual Studio
  • It's more flexible, so you can do more things without having to write or buy new controls
  • When you do need to use 3rd party controls, the developers of these controls will likely be more focused on WPF because it's newer
  • XAML makes it easy to create and edit your GUI, and allows the work to be split between a designer (XAML) and a programmer (C#, VB.NET etc.)
  • Databinding, which allows you to get a more clean separation of data and layout
  • Uses hardware acceleration for drawing the GUI, for better performance
  • It allows you to make user interfaces for both Windows applications and web applications (Silverlight/XBAP)

WinForms advantages

  • It's older and thereby more tried and tested
  • There are already a lot of 3rd party controls that you can buy or get for free
  • The designer in Visual Studio is still, as of writing, better for WinForms than for WPF, where you will have to do more of the work yourself with WPF