Category Archives: Windows Presentation Foundation

Update to "Taking the WPF Plunge"

Just a quick note, I made two updates to Monday’s post on Taking the WPF Plunge (https://arcanecode.wordpress.com/2007/08/13/taking-the-wpf-plunge/), one clarified a point and the second added one more download item you may want. If you’ve already read it, please go take a quick look at the updates.

Arcane

WPF and XAML

XAML is the definition language most often used to define a WPF based user interface. Using XAML you can define sleek, modern and highly functional applications.

Most people talk about XAML and WPF interchangeably, but I feel I need to kick off our discussion with an important concept:

XAML != WPF

XAML and WPF are, in fact two separate things even if they are closely linked. XAML is a definition language, similar to HTML. Well, more like HTML, WINForms and XML collided together in a Mythbusters type of explosion, and XAML is the result.

XAML requires a framework to execute. Often that framework is WPF, but it’s not necessarily so. Internet Explorer can interpet and display a XAML page, as can tools like XamlPad. Take this simple example:

<Page xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation&#8221; xmlns:sys=”clr-namespace:System;assembly=mscorlib” xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml&#8221; >
<Grid>
<Label>Welcome to ArcaneCode!</Label>
</Grid>
</Page>

Here you can see XamlPad displays the label in the upper part of the window:

clip_image002

No WPF, no compilation, no other magic, just the XAML Parser at work. You could save the code snippet above, and open it in Internet Explorer if you so wished.

Now let’s create another user interface, this time we’ll use WPF and NO XAML! Start a new project in Visual Studio, and pick BlankProject (note, do NOT pick Windows Project (WPF). We’ll use it later, but not today.)

Add references to three assemblies in the .Net tab: PresentationCore, PresentationFramework, and WindowsBase.

Now add a class, I named mine wpftest. Here’s my code:

using System;

using System.Collections.Generic;

using System.Text;

using System.Windows;

using System.Windows.Controls;

 

namespace wpf001

{

  class wpftest : Application

  {

    [STAThread]

    static void Main()

    {

      wpftest mytest = new wpftest();

      mytest.Startup += MyTestStartup;

      mytest.Run();

    }

 

    static void MyTestStartup(object sender, StartupEventArgs e)

    {

      arcaneWindow aw = new arcaneWindow();

      aw.Show();

    }

  }

}

Note the use of an arcaneWindow class, let’s create that now:

using System;

using System.Collections.Generic;

using System.Text;

using System.Windows;

using System.Windows.Controls;

 

namespace wpf001

{

  class arcaneWindow : Window

  {

    Label lblWelcome = new Label();

 

    public arcaneWindow()

    {

      this.Title = “Greetings!”;

      this.Width = 320;

      this.Height = 120;

 

      lblWelcome.Content = “Welcome to ArcaneCode!”;

      this.AddChild(lblWelcome);

    }

  }

}

Note the following things:

First, I’ve added two “using” lines, System.Windows and System.Windows.Controls. Next, I needed to create a new window (arcaneWindow) and inherit from the base Window type. Finally, I created a WPF label control, and added it to our window via AddChild.

Once you get all of it in, run the program and you should see:

clip_image003

There you go, you’ve just created a WPF user interface without a single line of XAML.

My point of this kick off was to demonstrate the difference between WPF and XAML. Most times, you will probably be using WPF and XAML in conjunction, but you don’t HAVE to.

In the coming days, we’ll delve a little deeper, and I’ll explain more of what some of the code you’ve seen today means.

WPF Resources

Yesterday I pointed out where to download all the bits you need to get into WPF. Today I thought I’d share some good resources on learning WPF.

First off is Todd Miranda’s site Xperimentality http://www.nxtdimension.com/blog/ . Todd is an MVP and has done many of the videos that appear on the Windows Client site.

Speaking of the WindowsClient.Net site, you can find many of those videos at http://windowsclient.net/learn/videos.aspx , just scroll down to the WPF area.

Todd Miranda’s videos on the Expression suite can be found on the Expression Knowledge Center site, http://www.microsoft.com/Expression/kc/resources.aspx?product=web&type=video . There are also videos by others.

Channel 9 has now reached 100 items on WPF: http://channel9.msdn.com/tags/WPF

Mark Miller recently did a DNRTV episode on Custom Controls in WPF, find it at http://www.dnrtv.com/default.aspx?showNum=72 . Also on DNRTV, Brian Noyes did a two parter on WPF, found here http://www.dnrtv.com/default.aspx?showNum=56 and here http://www.dnrtv.com/default.aspx?showNum=59 .

Another great blog is Lester’s WPF Blog, http://blogs.msdn.com/llobo/default.aspx . Lots of good stuff from the author of XAMLPadX, which I recommended yesterday.

Finally, a book recommendation, I’m finding Adam Nathan’s book on WPF to be a really good read. It’s more than technical enough to keep an experienced reading, and all of the code samples are in color! http://www.amazon.com/Windows-Presentation-Foundation-Unleashed-WPF/dp/0672328917/ref=pd_bbs_sr_1/103-4574773-3941455?ie=UTF8&s=books&qid=1187125352&sr=8-1

Taking the WPF Plunge

I wrote earlier that I felt WPF and XAML were the UI design platform of the future ( https://arcanecode.wordpress.com/2007/08/03/the-ui-of-the-future/ ). I’ve decided to put my time where my (some would say big) mouth is, and devote some time to learning WPF. In case you want to come along for the ride, lets look at what you need to get started.

First, you need the .Net Framework 3.0. Odds are you already have it, if you have Vista you definitely have it. If you are still on XP, and haven’t downloaded it grab your copy from http://www.microsoft.com/downloads/details.aspx?FamilyId=10CC340B-F857-4A14-83F5-25634C3BF043&displaylang=en . Make sure your XP has been patched with Service Pack 2.

That’s it, that’s all you have to have. However there are some things that will make your life easier. Visual Studio 2005, for example. Odds are if you read this blog you already have a version, but if not grab one from http://msdn2.microsoft.com/en-us/express/default.aspx . You’ll want to select Windows Development, and pick either C# or VB.Net.

Finally, you’ll want the Windows Software Development Kit. The Windows SDK contains a very useful tool called XamlPad, which you can use to quickly test your XAML code. The Vista version is at http://www.microsoft.com/downloads/details.aspx?FamilyId=C2B1E300-F358-4523-B479-F53D234CDCCF&displaylang=en . UPDATE (Aug 15): I should have been a little more awake when I wrote this. Even though the download has Vista in the title, the SDK will actually install and run on Windows XP (with SP2) and Server 2003 in addition to Vista.

If you’re on XP, or want something other than the SDK check out XamlPadX at http://blogs.msdn.com/llobo/archive/2007/04/23/update-xamlpadx-v2-1.aspx .

Finally, if you are lucky enough to have an MSDN Subscription, take a look at Expression Blend and Expression Web, I’ll be looking at them later.

There, that ought to keep you busy for a bit!

UPDATE UPDATE (Aug 16th)Well I did it again. When I posted the Aug 15th update (at the bottom) I grabbed the wrong text and pasted it in. Obviously, the Workflow Foundation extensions don’t do you any good when you want to work with WPF. Instead I meant to refer you to the extensions for WPF & WCF at http://www.microsoft.com/downloads/details.aspx?familyid=f54f5537-cc86-4bf5-ae44-f5a1e805680d&displaylang=en . Don’t let the fact that it reads CTP shake you, this is the last release for VS2005, Microsoft decided to make all further updates to the VS2008 product. The bits work fine though, and add templates so you can create WPF/WCF projects.

One note, when you go to install this extension, it will first recommend you have the Windows SDK (see link earlier in this message) installed. Second, it will recommend you set your help in Visual Studio 2005  to use local instead of on-line as the primary source. Just be aware of these two quirks.

I’ll leave the Workflow link below active though, it won’t hurt to install it, and you might want to dive into WF at some point.

UPDATE (Aug 15th): One more item you’ll want if you intend to use Visual Studio, you’ll want the Extensions for Windows Workflow Foundation, which you’ll find here http://www.microsoft.com/downloads/details.aspx?familyid=5D61409E-1FA3-48CF-8023-E8F38E709BA6&displaylang=en . (It’s only a 6 mb download, so it shouldn’t take long.)