3

I have a behavior that exposes several command properties. Typically I use MVVM Light and would just use a routed command in my view model and bind to those. However, this project is using Caliburn.Micro so I'm trying to do it the Caliburn.Micro way. Is there some syntax I can use on the Message.Attach to do this (is there any good documentation on the parser for Message.Attach)?

Do I have to modify the behavior to expose events to make this work with Caliburn.Micro?

Bryant
  • 8,660
  • 1
  • 33
  • 53

3 Answers3

4

Caliburn.Micro has its own mechanism for view/viewmodel communication as an alternative to commanding called Actions. You should be able to set the Action parameter to be your Execute delegate of your existing command, and if required set the appropriate view control property (e.g. IsEnabled) to bind to your CanExecute delegate.

devdigital
  • 34,151
  • 9
  • 98
  • 120
  • Here is a link to Actions as mentioned by devdigital,http://caliburnmicro.codeplex.com/wikipage?title=All%20About%20Actions – EtherDragon Jul 13 '11 at 16:59
  • Ok, your first sentence confused me, I thought you were porting code to Caliburn.Micro. In which case, just read the Actions article and you'll find most of the work is done for you via conventions. i.e. Call a button 'Send' and the Send method on your view model will be invoked when it's clicked. – devdigital Jul 13 '11 at 17:40
  • I'm trying to use a Behavior that is from another project. I was hoping to use the same Behavior but in the Caliburn Micro way which apparently is to not use Commands at all. So I rewrote the Behavior as a Trigger since that was easier. – Bryant Jul 13 '11 at 19:56
3

Caliburn.Micro handles routing Actions for your, as long as you setup the View and ViewModel correctly (it uses some implicit assumptions, which may or may not be your cup of tea) Here is a link about Actions: http://caliburnmicro.codeplex.com/wikipage?title=All%20About%20Actions&referringTitle=Documentation

A better, less tightly coupled way, of doing the same thing is to use the Event Aggrigator - http://caliburnmicro.codeplex.com/wikipage?title=The%20Event%20Aggregator&referringTitle=Documentation

Take a look at the HelloEventAggrgator code sample available in the Caliburn.Micro source, for an example... But the basic jist is this:

You make custom events for use by the aggregator.

public YourEvent
{
...
}

Your view will publish those custom events - it does not care who is listening, only that the event gets published.

public YourCodeBehind
{
    public Button_Clicked(...)
    {
        this.Events.Publish(new YourEvent());
        ...
    }
    ....
}

Your ViewModels will be setup to handle those events, by adding IHandle

[Export(typeof(...))]    
public class YourViewModel : IShell, IHandle<YourEvent>
{
    [ImportingConstructor]
    public YourViewModel(IEventAggregator events)
    {
        events.Subscribe(this);
        ...
    }
    public Handle(YourEvent event)
    {
        ...
    }
    ...
{

This method maintains very high SoC, by allowing the View to really only deal with binding to data, and publishing events - the view remains unconcerned about how the events are handled.

Each View Model is then setup to handle the Events by adding an IHandle interface. (Note that you can have many different IHandle interfaces on a single ViewModel) The ViewModel is unconcerned about how the event was raised, only that it was, and that it is the authority on handling that event from the Aggregator.

EtherDragon
  • 2,679
  • 1
  • 18
  • 24
  • 1
    I'm not sure how this relates to the question I asked at all. I'm trying to bind a command property on a Behavior. I don't have any events and I'm not going to make a custom event to handle this. – Bryant Jul 13 '11 at 19:54
  • 1
    Well, part of your quest asked how to do it the "Caliburn.Micro way" so that is the answer I offered. Sorry you didn't find it useful. – EtherDragon Jul 15 '11 at 19:47
  • Hey EtherDragon. I like the idea of using the Event Aggregator, but I'm a little confused by the code in your response. In the Button_Clicked handler you have "this.Events.Publish(new YourEvent());". My question is how do you get the view's code-behind to be aware of the Event Aggregator? – Kerby Sep 04 '13 at 17:17
-2

I ended up rewriting the Behavior as a Trigger instead to handle this.

Bryant
  • 8,660
  • 1
  • 33
  • 53
  • 4
    If you get a chance, could you post some sample code in this answer so we can refer to it in our projects? – EtherDragon Aug 05 '11 at 22:04
  • I don't think there is anything to share here really. Instead of using a behavior I used a trigger. – Bryant Mar 29 '13 at 14:54