Bug correction in Messenger and new feature in EventToCommand (MVVM Light Toolkit V3 alpha)

Update (11/14/2009): The sample application is available as a zip file, and I also published the Silverlight version to be executed directly in your browser.

MVVM_Black

The advantage of pushing very early software to the advanced users is that bugs are detected before too many people suffer from them, and features requests can be placed and implemented without pain. This is exactly what happened with the MVVM Light Toolkit V3 alpha 2.

Bug correction

This applies to the new “send with token” feature of the Messenger. When a recipient registers for a type of message, it can provide a token (any object). Then, when a sender sends a message, it can also provide the same token. The Messenger will only send the messages to those recipients who provided the same token when registering. This enables creating separate optimized communication channels between objects. The token can be any object, for example a string, a GUID, etc…

A silly mistake I made in the Messenger preventer simple values (such as an Integer) to be used as a token when sending a message. Simple values can be very valuable for this scenario, since a common reference is not needed to the token: It is simply a kind of contract that the sender and the receiver agree upon in advance. This is as decoupled as it gets, and yet enables a private channel of communication between two objects. Anyway, this mistake is corrected, and available in changeset 34855 on Codeplex.

New feature

A user on Codeplex required an additional feature in the EventToCommand behavior: Adding a way to get the EventArgs from the fired event to the invoked command. I tried to find a good way to implement that, and decided to make this an opt-in feature. The scenario is:

  • if you set the new property PassEventArgsToCommand to true in the EventToCommand
  • and you do NOT set the CommandParameter, nor the CommandParameterValue
  • and you use a RelayCommand<EventArgs> as the data bound command
  • then the parameter passed to the RelayCommand will be the EventArgs.

I believe that passing the EventArgs straight to the ViewModel is a bit overstepping the separation of concerns between the View and the ViewModel. However I do also acknowledge that there are cases where this can be useful, and so now it is possible. This feature is also implemented in changeset 34855 on Codeplex.

I also updated the sample application which can be downloaded here, or from Codeplex (see below). The Silverlight version of the sample application can also be run here.

Sample applications on Codeplex

I took the occasion to upload some sample applications on Codeplex too.

  • EventToCommand: This application demoes the feature of the same name, and is detailed in this blog post.
  • CleanShutdown: This application shows a practical use for the Messenger, and a way to implement a clean shutdown that can easily be extended with additional components. More about this application here.
  • RaiseCanExecuteChanged: This app shows how to use RelayCommands and more info about this sample is found in this blog post.

Happy coding!!

 

Print | posted on Friday, November 13, 2009 11:55 AM

Feedback

# re: Bug correction in Messenger and new feature in EventToCommand (MVVM Light Toolkit V3 alpha)

left by Laurent Kempé at 11/13/2009 9:53 PM Gravatar
Nice! Is this the new logo of MVVM Light?

# re: Bug correction in Messenger and new feature in EventToCommand (MVVM Light Toolkit V3 alpha)

left by Laurent Bugnion at 11/14/2009 9:47 PM Gravatar
Hey Laurent,

Yes it is. I wrote more about it here:
http://blog.galasoft.ch/archive/2009/11/07/mvvm-light-toolkit-logo-by-p.-schutz.aspx

Cheers,
Laurent

# re: Bug correction in Messenger and new feature in EventToCommand (MVVM Light Toolkit V3 alpha)

left by Mark Stega at 11/16/2009 7:23 AM Gravatar
I’ve had something go wacky with the application that I am building with respect to the view model locator. I am curious if anyone has seen this issue:

In App.xaml I define the resource for the locator class:

<Application.Resources>

<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Assets/Styles.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>

<!--Global View Model Locator-->
<vm:ViewModelLocatorClass
x:Key="Locator" />

</Application.Resources>

If I run the app via F5 or CTRL-F5 everything is OK and the VM’s are found. Seconds later VS2008 underlines from “<vm:ViewModelLocatorClass” to “/>” and shows an error of “The property ‘Resources’ is set more than once.” Clearly this error can be ignored but I am curious why it occurs.

# re: Bug correction in Messenger and new feature in EventToCommand (MVVM Light Toolkit V3 alpha)

left by Laurent Bugnion at 11/17/2009 8:18 PM Gravatar
Hi Mark,

You need to set all the resources within the ResourceDictionary tag. This syntax is a bit weird, I agree, especially since setting the resources outside of ResourceDictionary works fine in runtime mode, but Blend and Studio want only one item within the Applicaton.Resources (or Window.Resources, etc...) tag.

<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Assets/Styles.xaml"/>
</ResourceDictionary.MergedDictionaries>
<!--Global View Model Locator-->
<vm:ViewModelLocatorClass
x:Key="Locator" />
</ResourceDictionary>
</Application.Resources>

# re: Bug correction in Messenger and new feature in EventToCommand (MVVM Light Toolkit V3 alpha)

left by Bill Campbell at 11/20/2009 1:48 AM Gravatar
Hi,
This is really great stuff! Thanks!
I was wondering if you might give a quick example of how to add MVVM Light to an existing project? I have an existing mvvm project that I would like to extend using mvvm light. I know that I need to ref the dll's and to have my viewmodels derive from your viewmodelbase class but it starts to get a little confusing with the viewmodellocator and anything else that might be required.

thanks!
Bill

# re: Bug correction in Messenger and new feature in EventToCommand (MVVM Light Toolkit V3 alpha)

left by Laurent at 11/20/2009 9:45 AM Gravatar
Hey Bill,

Thanks a lot for the nice words.

Yes I do have a blog post started which will describe exactly this. It will take me a bit longer to write, but I intend to publish that as soon as I can.

Cheers,
Laurent

# re: Bug correction in Messenger and new feature in EventToCommand (MVVM Light Toolkit V3 alpha)

left by Laurent Bugnion at 11/24/2009 8:48 PM Gravatar
CheersThanks for the good words, really appreciate it! It was exactly the purpose of the toolkit, to have something easier to start with, and that can integrate with other solutions and be extended easily.

Cheers,
Laurent
Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification: