Category Archives: Events

PDC09: some WPF sessions are available !

PDC09

Earlier today, I mentioned that PDC09 videos will be available soon.

The first WPF videos are now available ! This article will be updated once new videos become available:

  • How Microsoft Visual Studio 2010 Was Built with Windows Presentation Foundation 4 [VIDEO HERE]
  • Advanced Windows Presentation Foundation Application Performance Tuning and Analysis [VIDEO HERE]
  • Mastering WPF Graphics and Beyond [VIDEO HERE]
  • Windows Presentation Foundation 4 Plumbing and Internals [VIDEO HERE]
  • XAML Futures in Microsoft .NET Framework, Microsoft Silverlight and Tools [VIDEO HERE]

PDC09: some announcements

PDC09

Some announcements have been made yesterday during PDC09:

– Silverlight4 Beta is available to download. The list of new features is available here, here are the most important:

  • Mic and webcam support
  • Printing support
  • Right click support
  • Binding improvements (we can now make binding between DependencyObjects, for example we can bind a TextBox and Slider directly in XAML)
  • Improved command support (to use MVVM !)
  • Advanced text editing control
  • Clipboard API
  • Google Chrome support
  • And lot more…

– The surface SDK is publicly available for download: http://msdn.microsoft.com/en-us/library/ee804845.aspx

– Office2010 Beta is available for download: http://www.microsoft.com/france/office/2010/beta/default.mspx

Many WPF sessions are going to be available for streaming in the next couple of days:

  • How Microsoft Visual Studio 2010 Was Built with Windows Presentation Foundation 4
  • Advanced Windows Presentation Foundation Application Performance Tuning and Analysis
  • Deep Dive into WPF4 Multi-Touch APIs
  • Mastering WPF Graphics and Beyond
  • Windows Presentation Foundation 4 Plumbing and Internals
  • Microsoft Surface: Multi-touch Development Integration with Windows 7 and WPF 4
  • XAML Futures in Microsoft .NET Framework, Microsoft Silverlight and Tools

If you wan an overview of the incoming new features for WPF4, check out Lester’s blog.

Another information, not PDC related, about a new very cool WPF data-visualization application made by Microsoft: Pivot.

pivot

Check out more information here.

How to close a View from a ViewModel ?

Note: I wrote this article to clarify relationships between ViewModel and View classes. If you want a complete solution you should take a look at existing MVVM framework like Cinch (by Sacha Barber).

Yesterday, there was an insteresting question about MVVM on StackOverflow: “How to close a View from a ViewModel ?”

Like always with WPF, there are many approaches to solve this problem.

Solution 1: give a reference to the View in the ViewModel

You need to control the View from the ViewModel ? Just gives a reference to the View in the ViewModel constructor.

public Window1()
{
  this.DataContext = new Window1ViewModel(this);
}

Unfortunatelly, this approach has several drawbacks:

  • it breaks the foundamental principle of the MVVM methodology: the ViewModel should be an abstraction of the View
  • it complicates the work needed to unit test your ViewModel
  • it introduces high coupling between the ViewModel and the View

Solution 2: the ViewModel raises an event when it wants to close its associated View

If having a reference to the View in the ViewModel is not the right thing, why not using an event. We can add a RequestClose event in the ViewModel class and raise this event when the ViewModel wants to close its associated View.

The View, when it creates the ViewModel subscribe to the RequestClose event. In the event handler, the View is closed.

// class is omitted, only constructor is shown
public Window1()
{
  var viewmodel = new Window1ViewModel();
  viewmodel.RequestClose += (s, e) => this.Close();

  this.DataContext = viewmodel;
}

Solution 2, first refinement

Of course I prefer the event based solution, but we can improve it. The first refinement we can do is to make sure the event will be coherent over all our classes. To achieve this I setup an interface:

public interface IRequestCloseViewModel
{
  event EventHandler RequestClose
}

This interface is implemented by my ViewModel classes which wants to support the ability to close their associated Views.

Solution 3, second refinement

Another possible refinement is to automate the subscription of the RequestClose event in the View. To do that, I created an abstract ApplicationWindowBase class that inherits from Window. When the DataContext changes, I check if the IRequestCloseViewModel is supported by the DataContext:

public class ApplicationWindowBase : Window
{
  public ApplicationWindowBase()
  {
    this.DataContextChanged += new DependencyPropertyChangedEventHandler(this.OnDataContextChanged);
  }

  private void OnDataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
  {
    if (e.NewValue is IRequestCloseViewModel)
    {
      // if the new datacontext supports the IRequestCloseViewModel we can use
      // the event to be notified when the associated viewmodel wants to close
      // the window
      ((IRequestCloseViewModel)e.NewValue).RequestClose += (s, e) => this.Close();
    }
  }
}

Like I said in the intro, this a very basic implementation of this concept. Many other approach exists. A good source of information is in the source code of existing MVVM frameworks.