# Sunday, March 05, 2006

Event bubbling or event routing

WPF introduces EventBubbling. People who have done scripting in Internet Explorer will find this very familiar. The concept is that an eventhandler may not be implemented on a GUI-control, but can also be implemented on the parent of that GUI-control. When an event is fired it will traverse the tree from child to parent to parent to parent until it reaches the top control, usually the window.

Let's look at a demo.

Below is the XAML file for Window1.

<Window x:Class="Demo3.EventHandling.Window1"
    xmlns="http://schemas.microsoft.com/winfx/avalon/2005"
    xmlns:x="http://schemas.microsoft.com/winfx/xaml/2005"
    Title="Demo3.EventHandling"
    >
    <Grid>
    <Button VerticalAlignment="Stretch" Click="button1_Click"
            HorizontalAlignment="Stretch" Grid.Column="0" Grid.ColumnSpan="1"
            Grid.Row="0" Grid.RowSpan="1" Margin="80,85,77,121"
            Width="NaN" Height="NaN" Name="button1">
      <StackPanel Orientation="Vertical">
        <Image Source="C:\Documents and Settings\Mark\Desktop\banner.jpg" Name="image1"/>
        <TextBox Name="textbox1" Text="Type your name here!" Height="20" Width="333" />
        <Button Name="innerButton" Click="innerButton_Click">Press me!</Button>
      </StackPanel>
    </Button>
  </Grid>
</Window>

Here is the code.

void button1_Click(object sender, RoutedEventArgs e)
{
    MessageBox.Show("Hello " + this.textbox1.Text);
    Button source = e.OriginalSource as Button;
    if (source != null)
    {
        MessageBox.Show("The original source is: " + source.Name);
    }
}

void innerButton_Click(object sender, RoutedEventArgs e)
{
    MessageBox.Show("Hello SDN!");
}

This will look like:


Just click on the various controls. Notice that controls that do not have a 'Click' event do not participate in the routing, but may still pass the event on.

In WPF this concept is called event routing. You can halt the routing of an event by setting

e.Halted = true;

 

 

#    Comments [0] |

Application Object

In the System.Windows namespace there is an 'Application' object. Your application should make use of this class since it allows for lifetime tracking through events like StartUp and SessionEnding and methods like Run.

We can expand our hello world to:

using System;
using System.Windows;


namespace HelloWorld
{
    public class MyApp : Application
    {
        [STAThread]
        static void Main(string[] args)
        {
            MyApp app = new MyApp();
            app.Startup += app.OnApplicationStart;
            app.Run(args);
        }


        void OnApplicationStart(object sender, StartupEventArgs e)
        {
            Window w = new Window();
            w.Title = "Mark says: Hello World!";
            w.Show();
        }
    }
}

With a little bit of digging around you can find the code that Visual Studio uses for initializing the application:


public partial class MyApp : System.Windows.Application
{
    /// <summary>
    /// InitializeComponent
    /// </summary>
    public void InitializeComponent()
    {
        this.StartupUri = new System.Uri("Window1.xaml", System.UriKind.Relative);
        System.Uri resourceLocater = new System.Uri("myapp.baml", System.UriKind.RelativeOrAbsolute);
        System.Windows.Application.LoadComponent(this, resourceLocater);
    }



    /// <summary>
    /// Application Entry Point.
    /// </summary>
    [System.STAThreadAttribute()]
    public static int Main(string[] args)
    {
        System.Threading.Thread.CurrentThread.SetApartmentState(System.Threading.ApartmentState.STA);
        MyApp app = new MyApp();
        app.InitializeComponent();
        return app.Run(args);
    }
}

As you can see some resources are initialized in the InitializeComponent. By default the VS2005 WinFx project sets up some resources, you find them under the 'Properties' folder. Also an URI is set to track the initial window. This URI is the window that will be started when app.Run(..) is executed.

 

#    Comments [0] |

More Hello World...

Time to continue with WPF.

You don't need to use XAML to program WPF. Quite the contrary. Everything you can do with XAML you can do with C#. It's just a matter of using the right tool for the right job. It doesn't make much sense to do all the layout code in C#, XAML will hopefully do a better job there. Anyway, just to prove the point I have to start where every new technology starts: Hello World!

I've installed all the necessary tools. I've started VS2005 and created a new WinFx application.
The application by default holds references to all relevant assemblies, these being:
- PresentationCode
- PresentationFramework
- ReachFramework
- UIAutomationProvider
- UIAutomationTypes
- WindowsBase

The names of these assemblies don't look very RTM-like, so I have to assume they'll change in one of the upcoming CTP's or RTM.

I like to start as clean as possible, so I throw out all the stuff under the properties folder and also the default created MyApp.xaml and Class1.xaml.

Now add a new class called HelloWorld:


using System;
using System.Windows;  //This is the WPF namespace

namespace HelloWorld
{
    public class HelloWorld
    {
        [STAThread]
        public static void Main()
        {
            // This is the System.Windows.MessageBox
            MessageBox.Show("Hello SDN!");           
        }

    }
}

Startup the application and go!

#    Comments [0] |
# Saturday, March 04, 2006

Is a programming languange a language if there is no tooling?

An interesting thread is going on at Ted's blog: http://blogs.tedneward.com/Trackback.aspx?guid=5f213ccd-5acb-4c81-9ec3-23b20db68447. I've put my 2 cents in, so read the comments.

#    Comments [0] |
# Thursday, March 02, 2006

Crossbow part of WPF

Mike Henderlight blogs that the "Crossbow"-technology will actually be part of WPF.

"We have spent the last several months working very hard on developing and executing on a plan where we could deliver the Crossbow runtime as an integral part of WPF.  As of the Beta2 release of WPF, the Crossbow runtime will be part of the WPF redist and reside in the GAC just like all of the other WPF components.  And from that point forward we will be on the WPF ship schedule which means that we will RTM with WPF."

This is great! The Crossbow-technology has been created by a seperate team within Microsoft, but I'm sure when WPF hits the streets most developers will assume this to be a very "oh yeah, there is interop with WinForms, what's new" kind of feature. You know? One that everyone expects to be there, but is really very very cool.

#    Comments [0] |
# Wednesday, March 01, 2006

Messenger Live Beta

Just installed Messenger Live Beta.
 
Looks okay, not too much new stuff here, except the 'Call' button next to a contact. Will MSN Messenger move into the VOIP space? A quick Google action results in:
 
"The phone function (result of a partnership between Microsoft and communications firm MCI) will be known as MCI web Calling for Windows Live Call and will allows subscribe to make pc to telephone calls at competitive rates."
 
It'll be interesting to see if Messenger can catch up with Skype. I had a video-phone call from the States to the Netherlands yesterday and the quality of Skype for voice and video are both higher than MSN Messenger.
I'll have to try Messenger Live to see if it has improved.
 
Contacts Live is also coming. This appears to be a similar service to Plaxo (www.plaxo.com). I don't have a MSN or Hotmail account so sadly I can't test it with my current passport.
 
 
 
#    Comments [0] |
# Tuesday, February 28, 2006

Introduction WWF

There will be a session about Windows Workflow Foundation (WWF) by Marcel de Vries on the upcoming Software Developer Event. Just to get a headstart have a look at this article by Dino Espito: http://msdn.microsoft.com/windowsvista/building/workflow/default.aspx?pull=/library/en-us/dnlong/html/wfgetstart.asp

#    Comments [0] |
# Monday, February 27, 2006

Developing for MS Outlook

Last december I did a presentation on using Visual Studio Tools for Office 2005 to develop Outlook add-ins. I just received an email from my friend Lucas with a bunch of more recent links on this topic.

Here it is:

Extending Enterprise Applications with Microsoft Outlook: Architectural Design Guide

Summary: Provides an architectural design and sample code that demonstrates an approach for integrating enterprise CRM and other LOB application data into the Microsoft Outlook user interface. (20 printed pages)

http://msdn.microsoft.com/architecture/default.aspx?pull=/library/en-us/dnbda/html/OtlkLOBCRM.asp

Synchronizing a Local Data Store with Microsoft OutlookSynchronizing a Local Data Store with Microsoft Outlook

Summary: Utilize a SQL Express database as a local data cache and the programmability of Microsoft Outlook to integrate enterprise CRM data within the Outlook user interface. (25 printed pages)

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnbda/html/OtlkLDSSynch.asp

Outlook Customization for Integrating with Enterprise Applications

Summary: Improve the availability of CRM data for information workers by creating a sample Outlook add-in and associated set of utility classes using Visual Studio Tools for Office that displays enterprise CRM data naturally within the Outlook user interface. (30 printed pages)

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnbda/html/OtlkCustInEntApp.asp

Sample

Microsoft has developed a sample Outlook add-in solution that demonstrates how the Outlook UI can be extended and customized to create a front-end for a CRM system. For more information about the "CRM Integration Add-in Sample for Microsoft Outlook" see http://download.microsoft.com/download/9/9/C/99CD8598-2A46-48A8-9A5B-7A30D46C0856/CRM Integration Sample for Outlook Source Setup.msi.

 

#    Comments [0] |
# Friday, February 24, 2006

SmtpMail.Send(...)

I was coding a little tool to send our SDN newsletters and discovered the new SmtpMail stuff in .NET2.0. Now you don't need to code any stuff for setting your mailserver or credential. Just configure it!

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.net>
    <mailSettings>
      <smtp deliveryMethod="Network">
        <network host="mail.myserver.net"
          port="25" 
          userName="Mark"
          password="secret"/>
      </smtp>
    </mailSettings>
  </system.net>
</configuration>

Now just send the message using the new classes in System.Net.Mail:

System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient();
client.Send( message );
#    Comments [0] |
# Thursday, February 23, 2006

Crossbow part of Feb CTP

The February CTP build of the WinFX SDK and also the Orcas February CTP will both feature Crossbow.

Crossbow will allow you to use WPF functionality on existing Windows Forms.

Read more on: http://blogs.msdn.com/mhendersblog/archive/2006/02/01/522723.aspx

Update:

There is also an MSDN show on MSDNtv: http://msdn.microsoft.com/msdntv/episode.aspx?xml=episodes/en/20060216CrossbowMH/manifest.xml

#    Comments [0] |

Windows Vista February Community Technology Preview (CTP)

The February CTP is now available for download, also the WinFX release candidate is available on the same page:

http://msdn.microsoft.com/windowsvista/getthebeta/default.aspx

A list of breaking changes between this and the January CTP has been posted here:

http://windowscommunication.net/collateral/pages/BreakingChangesJanCTPToFebCTP.htm

#    Comments [0] |
# Tuesday, February 21, 2006

Coding Guidelines

Today I had a discussion about coding guidelines and in particular about the size of a class, the number of methods in a class and the number of lines of code per class. What is right?

Ofcourse a little search on Google revealed millions of hits. Some of the more useful ones were:

Most standards don't say much about the size of a class, the number of methods in a class and the number of lines of code per class. The few that do say something come fairly close.

Here's the sum of guidelines that I think I'll stick by:

  1. A source file should only contain one class
  2. A method should be no more than 30 lines of code, excluding blank lines and comments
  3. A source file should contain no more than 1000 lines, including blanks and comments
  4. An interface should have no more than 10 methods, excluding overloads, ie. the combination of overloads counts as one methods
  5. C# 2.0 allows partial classes:
    • a source file should only contain one partial class
    • don't split the implementation of an interface across multiple files
    • for large classes split the implementation of interfaces and implement one interface per file
      • Filename:  <classname>.<interfacename>.cs
    • if you have a class which implements one interace, but has a lot of private methods, then create a partial class with just the private methods:
      • Filename: <classname>.private.cs

Ad 2.
This is the average I found and seems to make sense, since this means that with one look at the screen your brain can absorb the whole method.

Ad 3.
I'm not convinced this is a very important rule. Having 2000 lines of code in one file or spread across two classes in two files doesn't really make that big a difference to me. See also ad 5.

Ad 4.
This is mainly because of Intellisense. Ten methods will provide optimum use of Intellisense.

Ad 5.
Partial classes are great!

#    Comments [2] |
# Monday, February 20, 2006

Ten reasons to come to Software Developer Conference 2006


I'll be speaking at the Software Developer Conference 2006. The biggest software developer usergroup meeting in The Netherlands and probably Europe. Are you going? If not, why not?

Ten reasons for coming to SDC 2006:

- The schedule: 90+ sessions about software development, software development and more software development, and to sum it up: more software development.
- The speakers: the best of best, the cream of the crop from all over the world!
- The sessions: educational, in depth, fun and repeated so you don't have to miss any.
- The evenings: the monday nights at SDC are always great, for now still a suprise but it will be very amusing!
- The Internet Cafe: there will be Wi-Fi at the conference, but for those who don't have a laptop with them there will be the Internet Cafe.
- All-in price: the SDN is a no-nonsense usergroup, so no surprises and a crazy value for money conference. The registrationfee covers everything except for the bartab on Monday night.
- Supplier access: all important industry supplies will be present and available for questions.
- Goodiebag: with notebook, nice bag, extra's and extra's...
- Location: to be at Papendal for two days is a great environment to leave your work behind and submerge in the sessions and the wealth of knowledge which will be shared.
- Price: as a usergroup the SDN does not organize this conference for profit, but for you, the developer!

Want more information? Go to: www.sdc.nl

Ofcourse you're going! See you there!

#    Comments [0] |
# Saturday, February 11, 2006

Getting started with the Windows Presentation Foundation beta

So you're a developer and you want to get started with Windows Presentation Foundation (a.k.a.) Avalon?
Well here are the steps I took to play around with this new and hip technology:

  1. Create a fresh VirtualPC image with WindowsXP SP2. I read that you can also use Windows Server 2003 if you wish.
  2. Install Visual Studio 2005. I use Professional edition, but the beta's also work with the Visual Studio Express editions.
  3. Download the WinFX software developer kit at: http://msdn.microsoft.com/windowsvista/getthebeta/default.aspx 
    I've downloaded the WinFX RTC plus the VS2005 extensions.
    Note that if you want to install the VS2005 extensions you also need to download the Windows SDK beta.
    If you don't install the SDK the VS2005 extensions will show a message saying that you need to install the Windows SDK and then also make sure that you install the beta SDK, the regular SDK will not suffice!
    Feels like my WindowsXP machine is being turned into a Vista machine already :-)
  4. Install the WinFx RTC download. The installer will want to download another 16MB of data, so make sure your VirtualPC environment has Internet access.
  5. Install the Windows SDK beta.
  6. Install the VS2005 Extensions package.
  7. Install the VS2005 Extensions for Windows Workflow Foundation. These are obviously optional for playing around with the WPF beta, but since we're setting up a play area we might as well get it complete.

Done? Okay, time to test to see if all is well.

Make a little test XAML application, it ofcourse has to be:

a) Use notepad to create a file called 'test.xaml'
b) Copy code below into file.

<Page xmlns="http://schemas.microsoft.com/winfx/avalon/2005"
xmlns:x="http://schemas.microsoft.com/winfx/xaml/2005">
   <TextBlock>Hello World!</TextBlock>
</Page>

c) Save
d) Double click the file.
e) Yeah! A real life XAML application.

I also tried placing this test file on my webserver ( http://www.develop-one.net/presentations/code/wfm/test.xaml ) but opening this URL results in a security exception. I'll look into that some more later. Probably still need to add this URL my Trusted sites.

 


 

#    Comments [0] |
# Wednesday, February 08, 2006

Speaking at the Software Developer Event, March 2006

I've just been confirmed as a speaker at the Software Developer Event, the 24th of March 2006 in Ede, The Netherlands. I'll be doing an exciting session about Windows Presentation Foundation, a.k.a. Avalon.

For more information read the session abstract below. Hope to see you there!

Working with Windows Presentation Foundation
This introductory session about Windows Presentation Foundation, formerly known as Avalon, consists of two parts. The first part is about the WPF fundamentals. Here the developer will get 'a look under the hood' in order to get a better understanding of what is happening when developing a WPF application. The second part of the session looks at two tools that can be used to actually develop a WPF application and be productive while doing it, because who really wants to manually program XAML, right? The first tool we'll look at is codenamed 'Cider'. This tool allows a developer to create WinForm-like applications with WPF. The second tool is codenamed 'Sparkle' and is used for creating more Flash-like applications.

#    Comments [0] |
# Tuesday, January 31, 2006

Team Foundation Server to ship in March 2006

At VSLive! Somasegar has announced that Team Foundation Server will ship in March 2006.

View the video of the keynote at VSLive! at http://www.ftponline.com/channels/net/reports/vslivesf/2006/soma/

#    Comments [0] |
# Monday, January 30, 2006

Design Guidelines for Windows Vista

Microsoft has released a draft version of the design guidelines for Windows Vista.

Surf to: http://www.microsoft.com/downloads/details.aspx?FamilyId=FD380553-911E-4659-A085-4DD58AE4B9AE&displaylang=en

#    Comments [1] |
# Tuesday, January 24, 2006

Expression Interactive Designer - preview available

Microsoft has released a preview of it's tool for editing XAML. The codename used to be Sparkle, the production name will be Expression Interactive Designer.

Read more on Manuel Clement's blog: http://spaces.msn.com/members/manodesign/Blog/cns!1pRsSVBaAuwgFSti-clSJ6Ng!384.entry

#    Comments [0] |