Tag Archives: wp7

MVVM Framework explorer updated

I just updated my MVVM frameworks explorer Silverlight application. You can find the updated application here.

Here is the top 5 of MVVM frameworks supporting WPF, Silverlight and Windows Phone 7:

  1. MVVM Light (61k downloads)
  2. nRoute (19k downloads)
  3. Caliburn Micro (18k downloads)
  4. Simple MVVM toolkit (5k downloads)
  5. Catel (5k downloads)

When enough ViewModel is enough

In the last few years, we’ve seen the WPF and Silverlight community embracing the MVVM methodology. As one of the early adopters of MVVM (one of my first post about the subject was late 2008), I’ve seen the pattern evolving both in the web community and with developers I’ve met in my daily life.

Today, I’d like to share with you a simple concept I try to stick to when I’m doing WPF, Silverlight or Windows Phone 7 development. It can be summarized as “Enough ViewModel is enough”.

The simple idea behind this slogan is that there ARE stuff which are view-related and SHOULD NOT be embedded in the ViewModel layer. I’ve seen too many developers going the “100% viewmodel way” which means for them absolutely no code-behind without any dispensation.

For example, I came across this code. It’s the ViewModel layer associated to a simple view where the user fills various input and has immediate feedback about the progress (like “75% of the fields are completed”). If by the way you’re interested in implementing this behavior you can check out this article I wrote on CodeProject)

The following code is simplified to the sake of the article:

public class ViewModel
{
    // data field acts as the model object behind the VM layer
    private Data data;

    // missing code...
    // public properties used by the view using databinding

    public ViewModel()
    {
        // very simplified for this article...
        this.data = new Data();
        this.data.SelectedValuesChanged += new EventHandler(data_SelectedValuesChanged);
    }

    public void UpdateProgress()
    {
        // some code...
    }

    void data_SelectedValuesChanged(object sender, EventArgs e)
    {
        this.UpdateProgress();
        this.Initialize();
    }

    public void Initialize()
    {
        var item = this.data.GetItem("id1");
        if (item != null)
        {
            item.PropertyChanged += new PropertyChangedEventHandler(item_PropertyChanged);
            item.SelectedValuesChanged += new EventHandler(item_SelectedValuesChanged);
            foreach (var value in item.Values)
                value.PropertyChanged += new PropertyChangedEventHandler(item_PropertyChanged);
        }

        item = this.data.GetItem("id2");
        if (item != null)
        {
            item.PropertyChanged += new PropertyChangedEventHandler(item_PropertyChanged);
            item.SelectedValuesChanged += new EventHandler(item_SelectedValuesChanged);
            foreach (var value in item.Values)
                value.PropertyChanged += new PropertyChangedEventHandler(item_PropertyChanged);
        }
    }

    void item_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
    {
        this.UpdateProgress();
    }

    void item_SelectedValuesChanged(object sender, EventArgs e)
    {
        this.UpdateProgress();
    }
}

The idea is simple, as soon as the user changes a value in the View, we must compute the current progress. Because the ViewModel have several levels, we end-up having to register to every single PropertyChanged event which leads to cumbersome code. By the way, this code can also creates memory leaks since we register a lot of event handlers without removing them, but that’s another discussion…

Here is my way to solve this problem:

public partial class View : UserControl
{
    private readonly ViewModel viewmodel;

    public View()
    {
        InitializeComponent();

        this.viewmodel = new ViewModel();

        this.AddHandler(
            FocusManager.LostFocusEvent,
            new RoutedEventHandler(this.OnLostFocus),
            true);
    }

    private void OnLostFocus(object sender, RoutedEventArgs e)
    {
        this.viewmodel.UpdateProgress();
    }      
}

What is wrong with this way ? The View has code-behind ? That’s not a big deal: the code is more readable, maintainable. It makes also more sense: when a view-related operation occurs (in this case, focus has changed), update the progress.

The simple message I’d like to spread is the fact that there is nothing wrong with the fact to have sometime, a little bit of code-behind in your view if that facilitates your architecture. There is no need to create a complicated infrastructure with behaviors, commands or bindings just to keep the view empty if that does not make sense.

Another great example in a real application is available in the Advanced MVVM book by Josh Smith.

 

Mix11: Windows Phone 7 news

Today is Mix day ! After yesterday keynote about HTML5, IE10 preview and other web stuff, it’s time for serious Silverlight and Windows Phone announcements !

Today’s keynote starts with Joe Belfiore talking about the update process for the NoDo update of WP7. As you know this update was not delivered “properly”, but at least we know have a good communication from Microsoft about that (you can for exemple check-out the Where’s my phone update website)

Nokia partnership

  • Marco Argenti, Head of Dev Experience comes on stage
  • Nokia is “working very hard” to deliver the very first WP7 device

General

  • update is schedule for the fall
  • all existing phones and new phones will get the update
  • 16 additional languages will be supported
  • more countries where we can create apps (from 30 to 38)
  • more countries where we can buy apps (from 16 to 35)

Marketplace / Search

  • better apps list: filter by letters (like with the contacts)
  • new search button to search for a particular app
  • better marketplace
    • more information with less confusion (music and apps are no longer mixed)
    • search for podcasts
    • new “related” pivot item when seeing an app for related app
  • we can integrate an app with the search feature of the phone

Web Experience

  • the update include IE9
  • the core engine is the same code base that the one we found desktop : including HW acceleration
  • address bar is moved to the bottom
  • support background audio for HTML5: you can leave IE and keep listening to the music !
  • tabbed UI for navigating between tabs
  • support for H264 video

Platform

  • improved panorama and pivot control
  • improvements for live tiles
  • access to the ring tones settings (set a music files as ring tone programmatically)
  • TCP/IP sockets
  • SQL database
  • more launchers and choosers
  • data access to the contacts and calendars on the phone
  • better integration with the sensors on the phone (like RAW camera access)
  • Skype coming to the platform this fall (leveraging many of those new features)
  • pin live tiles with deep link to our apps (which would launch the app and give the link as parameter)
  • Silverlight 4 features support
  • applications can now contains both Silverlight and XNA content

Multitasking

  • Spotify coming to WP7 using new multitasking capabilities
  • fast-app switching
  • use “live agents” to prevent battery life

Tooling

  • new version of tools will be available next month !
  • new capabilities in the emulator: simulate accelerometer and location sensors right from the emulator

  • new profiling support

Performance

  • scrolling: totally smooth scrolling without any changes in our applications !
  • image decoding
  • garbage collection
  • memory usage

Conclusion

  • A lot of new features available to developers + Silverlight 4 features coming to the phone
  • An development experience which is definitively the best out on the market for mobile development
  • Microsoft is listening to our feedback
  • Update is schedule for this fall !
  • New version of the tools next month