Question

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 -)

Was it helpful?

Solution

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.

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top