views:

2479

answers:

3

Hello! Please, don't judge strictly if this question was discussed previously or indirectly answered in huge nearby prism and mvvm blogs. In WPF implementation of RelayCommand or DelegateCommand classes there is a such eventhandler

         /// <summary>
 /// Occurs whenever the state of the application changes such that the result of a call to <see cref="CanExecute"/> may return a different value.
 /// </summary>
 public event EventHandler CanExecuteChanged
 {
  add { CommandManager.RequerySuggested += value; }
  remove { CommandManager.RequerySuggested -= value; }
 }

but in SL subset of namespaces there are no CommandManager class. And this is where I'm stuck. I haven't yet found an workaround for this in MVVM adoptation for SL (PRISM is so complex for me yet). Different simple HelloWorldMVVM apps don't deal with at all.

Thanks in advance and sorry for my English -)

+3  A: 

There is no support for commands in Silverlight. When the user manipulates controls in your view you will have to write code (e.g. event handlers) that modifies the view-model in the code-behind for your view. This might be something as simple as calling a method on the view-model when the user clicks a button in the view. By using PRISM you are able to create attached properties in the XAML for your view to get rid of these event-handlers, but if you would rather not use PRISM you can simply stick to using event handlers.

One other aspect you will have to handle is modifying the view when the view-model changes. In particular you will want to enable and disable controls based on the state of the view-model. To achieve this you will have to bind the IsEnabled property of a control to something in the view-model that reflects if a certain operation is allowed. Implementing custom IValueConverter objects that converts to boolean values are often useful. For instance, if your view-model has a property that represents a count and you want a particular control in the view to only be enabled when the count is greater than zero you can create a value converter that converts to true when the number is greater than zero and use this value converter in the binding.

If you try to adapt a WPF example of an MVVM application you will have to get rid of all uses of commands and substitute your own code. The code in your example is not meaningful in Silverligt, but in WPF it is involved in the process of determining if a control in the view is enabled, visible etc.

Martin Liversage
I think You are a little bit wrong - there is support of ICommand (may be limited, but it exists and the class ICommand exists) in Silverlight 2
Andrey Khataev
It is true that the `ICommand` interface is defined in Silverlight, but it is not used anywhere, and the entire commanding infrastructure in WPF that works so well with the MVVM pattern is absent in Silverlight.
Martin Liversage
Thanks. I thought that ICommand presence means full commanding support, so I was wrong -( So, it turns out that MVVM implementation in SL is not so easy, as in WPF. I found only one way by now - using attached propertis and behavoir just like this: http://blogs.southworks.net/jdominguez/2008/08/icommand-for-silverlight-with-attached-behaviors/or using ready framework, where many of events are embracedhttp://nroute.codeplex.com
Andrey Khataev
+1  A: 

Silverlight 4.0 supports the ICommand interface and thus will provide a WPF like commanding infrastructure.

dParker
A: 

I've written a blog post with an example of using the ICommand interface and MVVM in Silverlight 4 called How to Implement MVVM, INotifyChanged and ICommand in a Silverlight Application

Jag Reehal