Archive for the '.Net' Category

Discovering the potential of Windows Presentation Foundation

The user interface is an important aspect of software development that in most cases defines the first impression the customer gets of an application. We’ve seen lots of great software with poor, overly-simple interfaces, or on the contrary, sophisticated ones that sacrifice functionality, intuitiveness and/or efficiency. Windows Presentation Foundation (WPF) is Microsoft’s response to the need of 21th century-worthy user interfaces that are also easy to implement and use.

image

Why WPF?

As mentioned in Adam Nathan’s book ‘Windows Presentation Foundation Unleashed‘, there are six major benefits WPF has over the previous Microsoft technologies that shared its purpose (like GDI+, Windows Forms):

  • Broad integration. WPF integrates all the necessary technologies for developing 2D, 3D, audio, video, rich documents, and so forth, avoiding the need to learn, and implement several independent technologies.
  • Resolution independence. Due to the emphasis on vector graphics, WPF applications can be resolution independent, and contents of an application can automatically resize when the application itself resizes.
  • Hardware acceleration. Since WPF is built on top of Direct3D, everything from 3D graphics to text gets rendered by hardware, making every application -not only games- benefit from powerful video cards. However, if no powerful hardware is present, WPF still has a software rendering pipeline so that the hardware isn’t a requirement for WPF applications to run.
  • Declarative programming. The use of Extensible Application Markup Language (XAML) in WPF makes it possible to leverage the use of declarative programming to an incredible level of expressiveness, enabling graphic designers to contribute to not only the look and feel of the application, but also some behavior that is usually coded by developers.
  • Rich composition and customization. WPF controls are extremely customizable, so it is possible to dramatically alter the looks of any control without affecting its functionality.
  • Easy deployment. WPF provides support for deploying Windows applications through Windows Installer or ClickOnce, and even hosting them in a web browser (although the browser must be IE7 or further).

An interesting fact about WPF is that, apart from being part of the .Net Framework and designed for managed code, it is also implemented largely in managed code.

XAML

The introduction of this new markup language greatly simplifies the creation of a UI for the .NET framework, as it enables the use of declarative markup that directly represent the instantiation of managed objects, which can be later referenced through code-behind files. This is achieved by using partial class definitions; the window class is defined as a partial class when a window is created in XAML, and it’s explicitly defined as a partial class in the code-behind file.

This approach makes it possible to separate the UI runtime logic from the UI definition itself, which is an extraordinary benefit for making extensible, modular applications, especially if design patterns such as the ones facilitated by Prism are used.

Customization & Simplicity

At first sight, WPF may not seem easy to learn, since it introduces a lot of new concepts. But it has what I think is absolutely essential for any technology: it enables an extreme degree of customization, while keeping the simplicity of default values prepared for the most common scenarios. While I think both are very important, it’s not uncommon to see a technology sacrificing one to achieve the other. That’s what I think WPF accomplishes greatly, and I realize a lot of emphasis has been put in combining those two aspects. An example of this is the use of ControlTemplates: controls have a default template that defines their most usual appearance (such as a button being the typical grey rectangle we all know), but a custom template can be applied to completely redefine the way they look without altering, however, how they behave.

image

As for the learning curve, once you’ve grasped the main new concepts introduced in WPF (that is, the theory), everything else is really straightforward, and the initial overhead of learning those concepts is greatly compensated by the simplicity of creating applications with it.

Although XAML syntax may seem a little bit confusing at first sight, integration with Visual Studio and tools like Expression Blend makes it easy to develop applications without being an expert at all: just having an idea of what WPF is about is enough to start learning by trial and error and discover the whole new set of possibilities this technology offers!

.Net basics and C#

Last week I had a meeting with a Microsoft MVP, Martin Salias, in which he gave me an introductory explanation to the technologies I’m going to start using in Southworks. I’m very happy to be able to start my technical training with such a person, and I’ll make sure to put everything he teaches me to good use.

.Net Platform

He went over some history about the .Net Platform, and its role as an infrastructure that makes it possible to develop scalable, multi-platform applications in a variety of languages. The most remarkable features of the .Net Platform are the Common Language Runtime (CLR), and the Common Intermediate Language (CIL, formerly known as Microsoft Intermediate Language, MSIL).

Common Intermediate Language

It has been mentioned that .Net supports a variety of languages, such as C#, C++, Visual Basic.Net, and so forth; but in order to maintain interoperability, all the compilers of .Net applications related to any of these supported languages compile into a form of bytecode, which is in fact the CIL. It is the lowest level human readable code in the .Net Platform, and it can be thought of as a “generic” assembly-like code.

Common Language Runtime

Based on the Common Language Infrastructure, the CLR is a virtual machine that compiles the CIL code into native code, before actually running the program, or even progressively during runtime (which is known as Just In Time compiling). This step is important, because it allows the software developed in the .Net platform to be highly platform-independent. Not only programs written in different languages interact within the same environment as if they were written in the same language, but also a .Net based program can run in any device supporting the CLR, no matter which platform it was compiled in. In other words, .Net compilers create .exe and .dll files that look like normal compiled files, but the actual conversion into native code is done on the device that runs the program.

Base Class Library

The main purpose of supporting multiple languages in the .Net platform is for long time programmers to avoid abandoning the syntax and customs of the language they had been using for so long. So in this context, there is a large library of classes known as .Net Framework Class Libraries or just simply Base Class library (BCL), that nowadays groups most of the functionality of the .Net platform, and can be accessed by any .Net supported language. So now, the main difference between the several languages is just the syntax.

C#

In this context, C# is intended to be a simple, multi-purpose language that is greatly adapted to the .Net specifications and libraries, and is able to provide an interface to most of the .Net functionality. For example, C# doesn’t even have the capability of outputing text to the console, as this is comprised in the BCL.

I’m eager to keep studying this and I’ll post again as soon as I have learnt something interesting on the subject!