MVVM with Caliburn.Micro


Published on

With all the MVVM frameworks out there, it’s hard to know which one is best for you. In this session, we will look at one of the best MVVM frameworks out there: Caliburn.Micro. We will build an application from the ground up to show how to get started with Caliburn.Micro from the perspective that matters the most: the code.

Published in: Technology
  • Be the first to comment

No Downloads
Total Views
On Slideshare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide
  • Include MainView.xaml
  • Show Edit button XAML, how clicking it originally does nothing.Add Edit method to show convention
  • Add XAML for View method, then run and show exceptionThen add View method
  • $eventArgs – Passes the Trigger’s EventArgs or input parameter to your Action. Note: This will be null for guard methods since the trigger hasn’t actually occurred.$dataContext – Passes the DataContext of the element that the ActionMessage is attached to. This is very useful in Master/Detail scenarios where the ActionMessage may bubble to a parent VM but needs to carry with it the child instance to be acted upon.$source – The actual FrameworkElement that triggered the ActionMessage to be sent.$view - The view (usually a UserControl or Window) that is bound to the ViewModel.$executionContext - The actions's execution context, which contains all the above information and more. This is useful in advanced scenarios.$this - The actual ui element to which the action is attached.
  • AllFriendsViewModel - PublishMainViewModel – ReceiveAppBootstrapper - Subscribe
  • MainViewModel - Edit
  • AllFriendsViewModel – ScreenMainViewModel – Conductor -Implement Handle ViewPersonMessage RefactoredViewPersonViewModel and EditPersonViewModel – Close
  • MVVM with Caliburn.Micro

    1. 1. MVVM with Caliburn.MicroBrent Edwards
    2. 2. Brent EdwardsLead Consultant with
    3. 3. Level Set – Audience Background• XAML (WPF, Silverlight, Windows Phone 7, WinRT)• MVVM• Caliburn.Micro
    4. 4. Sample Application - Phonebook[Demo]
    5. 5. What Is MVVM?• Model-View-ViewModel
    6. 6. What Is Caliburn.Micro?• Small, powerful MVVM framework for XAML platforms• Technologies Supported – WPF 4.0 – Silverlight 4.0/5.0 – WP7 – WinRT/Metro•
    7. 7. How It Works• Some Configuration• Some Convention• Some Magic
    8. 8. Configuration: Bootstrapper• Application foundation• Configure Inversion of Control container• Bootstrapper<TRootModel> – Configures first View/ViewModel to show• By default, Nuget includes a Bootstrapper for MEF
    9. 9. Bootstrapper[Demo]
    10. 10. Convention: Views/ViewModels• View Resolution is ViewModel-First• Views/ViewModels wired together by convention• Entity naming convention – {EntityName}ViewModel  {EntityName}View• Namespace naming convention (Good practice) – ProjectName.ViewModels.{ViewModel} – ProjectName.Views.{View}
    11. 11. ViewModel-First[Demo]
    12. 12. Actions• What are they? – Call methods on ViewModel from event fired in View• 2 ways to use Actions 1. By Convention 2. Explicitly with Message.Attach
    13. 13. Actions - Convention• How it works – Name a Control in XAML – Create method on ViewModel with same name – Caliburn.Micro automagically wires them together • Remember the magic I mentioned?• Positives • Negatives – Very easy to develop – No runtime errors if names – Keeps XAML clean don’t match – No design-time support
    14. 14. Action Convention[Demo]
    15. 15. Actions - Explicit• How it works – Add attached property Message.Attach – Specify the event, method to call, and even parameters• Positives • Negatives – No magic – None that I’ve found • Other devs can easily see what you are calling – Simple parsing gives great flexibility
    16. 16. Message.Attach[Demo]
    17. 17. More Message.Attach<Button Content="Remove" cal:Message.Attach="[Event Click] = [Action Remove($dataContext)]" /><Button Content="Click Me" cal:Message.Attach="[Event Click] = [Action SayHello(Name.Text)]" /><Button Content="Click Me" cal:Message.Attach="SayHello(Name)" /><Button Content="Lets Talk“ cal:Message.Attach="[Event MouseEnter] = [Action Talk(Hello, Name.Text)]; [Event MouseLeave] = [Action Talk(Goodbye, Name.Text)]" />
    18. 18. More Message.Attach• Other argument options – $eventArgs – $dataContext – $source – $view – $executionContext – $this
    19. 19. Event Aggregator• What is it? – Single source for communication across the application
    20. 20. Event Aggregator[Demo]
    21. 21. Window Manager• What is it? – It um… Manages Windows ;)• Use it to show a window, a dialog, or a popup
    22. 22. Window Manager[Demo]
    23. 23. ViewModel Types• Screen – Most basic ViewModel type – Implements basic ViewModely stuff • INotifyPropertyChanged• Conductor – Controls activation/deactivation of multi-Screen views – Has a collection of ViewModels to display
    24. 24. ViewModel Types[Demo]
    25. 25. Questions?
    26. 26. Brent EdwardsLead Consultant with