Showing posts with label XAML. Show all posts
Showing posts with label XAML. Show all posts

4.1.10

A brief introduction to WPF

My previous post was an out-of-nowhere post on WPF (Windows Presentation Foundation), a technology not many developers know. Although you can easily know all there is to know "about" WPF on the web, learning to "use" WPF is more important and you need the proper resources for that. WPF applications can be developed in both Visual Studio 2008 and Expression Blend 2 and developing a complete WPF app requires expertise in both. Here are some resources which you can use:

A guided tour of WPF

The official WPF site

The MSDN resource for WPF

Here's my description of WPF:
WPF (Windows Presentation Foundation) is a programming model within the .NET Framework which allows you to build rich, compelling user interfaces. The highly-rated Silverlight platform for the Web is nothing but a subset of WPF. You can compare it to Windows Forms, although there is a world of difference between the two. In fact, WPF is best understood when compared to WinForms.
In a WinForms app, there are 1 or more "forms" which constitute the user interface. Each form is an instance of a class which derives from System.Windows.Forms.Form and its UI and codebehind are both in C#. So, there is a Form1.designer.cs and a Form1.cs both containing partial declarations of the class Form1.
In a WPF app, there are 1 or more "windows" which constitute the interface. Each window is an instance of a class which derives from System.Windows.Window. While the codebehind is in C#, the UI is in what is called XAML (Extensible Application Markup Language). So, there is a Window1.xaml containing pure XAML and a Window1.xaml.cs containing pure C#, both containing partial declarations of the class Window1.
XAML is an HTML-style language which allows you to layout the controls of a window just as you would layout elements on an HTML page. XAML provides what WinForms cannot provide - rich and highly customizable user experiences. XAML provides for custom ListBoxes, binding controls to XML and CLR data sources, animations through storyboards and plenty more. In fact, you will get a better idea once you actually start creating a WPF app in Visual Studio 2008.
Apart from Visual Studio 2008, there is a software called Expression Blend 2 which allows you to concentrate only on the UI through XAML. Its intuitive interface allows you to do whatever you wish with the user interface. WPF is a very good and recommended thing to learn for developers. Code hard, go pro!

27.12.09

Visual inheritance in WPF

My final year .NET project was a medium-weight WPF application which had some 8 windows in all. Creating the windows and customizing the interface in XAML was a breeze with Visual Studio 2008 and Expression Blend 2. Even coding up the window logic was not that tough, but halfway into the logic part, I realized that all my windows had some properties and methods in common. So the code could be shortened a great deal using inheritance.

Basically, the approach is simple. Create a class in a separate code file (say Blindow in Blindow.cs) which inherits from System.Windows.Window, add all the properties and method implementations and virtual methods you need to it, and make all your windows inherit from Blindow. So, our Blindow looks like this:


  1. namespace BlindApp.Windows
  2. {
  3. public class Blindow : Window
  4. {
  5. // all your properties and methods here
  6. }
  7. }


So, the class declarations for all the windows in the application now change from

public partial class Window1 : Window
to

public partial class Window1 : Blindow

All is fine and well, the code should compile correctly, but shockingly, it doesn't and throws up an error. Reason: Forgot the XAML. If you know WPF well, you must realize that the class declaration for Window1 in the codebehind is only partial. The remaining partial declaration lies in the XAML. So, this means one has to come up with a way to let XAML know that Window1 inherits from Blindow now.
The source of the error now lies here

<Window x:Class="BlindApp.Windows.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">


So what we need to do to rectify the error is first add an XML namespace (with any name you like) for the CLR namespace. The CLR namespace for Window1 is BlindApp.Windows, so add a line as follows

<Window x:Class="BlindApp.Windows.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
xmlns:blinder="clr-namespace:BlindApp.Windows">


You don't need to type out the clr-namespace: statement - Visual Studio 2008 provides IntelliSense for that. But, we are not done yet, an error will again pop up because we need to replace the first line itself. Now that the XML namespace has been created, this is how it's done:

<blinder:Blindow x:Class="BlindApp.Windows.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
xmlns:blinder="clr-namespace:BlindApp.Windows">


Just like you could set Resources and Triggers with Window.Resources and Window.Triggers, you can do the same now with <blinder:blindow.resources> and <blinder:blindow.triggers>