background preloader

Caliburn.Micro

Facebook Twitter

Caliburn Micro, MEF and multiple modules. Composable Application – Using MEF and Caliburn Micro - C# It ! Caliburn.Micro Soup to Nuts Pt. 1 – Configuration, Actions and Conventions. In this tutorial we will learn a few of the basics of Caliburn.Micro.

Caliburn.Micro Soup to Nuts Pt. 1 – Configuration, Actions and Conventions

Let’s start by getting the framework. Head on over to Either use Mercurial to clone the repo or click on the link for the latest change set, then click on the download link. I recommend that you go ahead and get setup with TortoiseHG for Mercurial. You can read here and/or watch a free TekPub video here for information on that. Once you have the source downloaded, navigate to the “src” folder. Caliburn.Micro Soup to Nuts Pt. 2 – Customizing The Bootstrapper. In the last part we discussed the most basic configuration for Caliburn.Micro and demonstrated a couple of simple features related to Actions and Conventions.

Caliburn.Micro Soup to Nuts Pt. 2 – Customizing The Bootstrapper

In this part, I would like to explore the Bootstrapper class a little more. Caliburn.Micro Soup to Nuts Pt. 3 – All About Actions. Caliburn.Micro Soup to Nuts Pt. 4 – Working with Windows Phone 7. Hopefully, previous articles have you up to speed on what Caliburn.Micro is, its basic configuration, and how to take advantage of a few of its features.

Caliburn.Micro Soup to Nuts Pt. 4 – Working with Windows Phone 7

In this part, I want to talk about some WP7 specifics issues. It’s unfortunate that I have to call out WP7, but your going to find that while you may be an experienced WPF or Silverlight developer, that doesn’t make WP7 development a snap. Microsoft still has a long ways to go in making “three screens and the cloud” a reality. The new features in Caliburn.Micro are specifically designed to address some of the shortcomings in WP7, particularly around Navigation (with ViewModels and Screen Activation), Tombstoning and Launchers/Choosers. Caliburn.Micro Soup to Nuts Pt. 5 – IResult and Coroutines. Before our WP7 detour, we were deep in the thick of Actions.

Caliburn.Micro Soup to Nuts Pt. 5 – IResult and Coroutines

I mentioned that there was one more compelling feature of the Actions concept called Coroutines. If you haven’t heard that term before, here’s what wikipedia* has to say: Here’s one way you can thing about it: Imagine being able to execute a method, then pause it’s execution on some statement, go do something else, then come back and resume execution where you left off. This technique is extremely powerful in task-based programming, especially when those tasks need to run asynchronously. For example, let’s say we have a ViewModel that needs to call a web service asynchronously, then it needs to take the results of that, do some work on it and call another web service asynchronously. First, notice that the Action “GoForward” has a return type of IEnumerable<IResult>. Public interface IResult { void Execute(ActionExecutionContext context); event EventHandler<ResultCompletionEventArgs> Completed; } [Advertisement]

Caliburn.Micro Soup to Nuts Part 6a – Screens, Conductors and Composition. Actions, Coroutines and Conventions tend to draw the most attention to Caliburn.Micro, but the Screens and Conductors piece is probably most important to understand if you want your UI to be engineered well.

Caliburn.Micro Soup to Nuts Part 6a – Screens, Conductors and Composition

It’s particularly important if you want to leverage composition. The terms Screen, Screen Conductor and Screen Collection have more recently been codified by Jeremy Miller during his work on the book "Presentation Patterns" for Addison Wesley. While these patterns are primarily used in CM by inheriting ViewModels from particular base classes, its important to think of them as roles rather than as View-Models. In fact, depending on your architecture, a Screen could a be a UserControl, Presenter or ViewModel. Caliburn.Micro Soup to Nuts Part 6b – Simple Navigation with Conductors. Previously, we discussed the theory and basic APIs for Screens and Conductors in Caliburn.Micro.

Caliburn.Micro Soup to Nuts Part 6b – Simple Navigation with Conductors

Now I would like to walk through the first of several samples. This particular sample demonstrates how to set up a simple navigation-style shell using Conductor<T> and two “Page” view models. As you can see from the project structure, we have the typical pattern of Bootstrapper and ShellViewModel. In order to keep this sample as simple as possible, I’m not even using an IoC container with the Bootstrapper. Let’s look at the ShellViewModel first. Public class ShellViewModel : Conductor<object> { public ShellViewModel() { ShowPageOne(); } public void ShowPageOne() { ActivateItem(new PageOneViewModel()); } public void ShowPageTwo() { ActivateItem(new PageTwoViewModel()); } } Here is the corresponding ShellView: Notice that the ShellViewModel has two methods, each of which passes a view model instance to the ActivateItem method.

Along with their views: Stay tuned, more samples to come… Caliburn.Micro Soup to Nuts Part 6c – Simple MDI with Screen Collections. Let’s look at another example: this time a simple MDI shell that uses “Screen Collections.”

Caliburn.Micro Soup to Nuts Part 6c – Simple MDI with Screen Collections

As you can see, once again, I have kept things pretty small and simple: Here’s a screenshot of the application when it’s running: Here we have a simple WPF application with a series of tabs. Clicking the “Open Tab” button does the obvious. Clicking the “X” inside the tab will close that particular tab (also, probably obvious). Public class ShellViewModel : Conductor<IScreen>.Collection.OneActive { int count = 1; public void OpenTab() { ActivateItem(new TabViewModel { DisplayName = "Tab " + count++ }); } }

Caliburn.Micro Soup to Nuts Part 6d – A “Billy Hollis” Hybrid Shell. Up until now I’ve been focusing on fairly simple usage of Screens and Conductors.

Caliburn.Micro Soup to Nuts Part 6d – A “Billy Hollis” Hybrid Shell

In this article, I want to show something a bit more sophisticated. This sample is based loosely on the ideas demonstrated by Billy Hollis in this well-known DNR TV episode. Rather than take the time to explain what the UI does, have a look at this short video for a brief visual explanation (apologies for the audio level). Ok, now that you’ve seen what it does, let’s look at how it’s put together. As you can see from the screenshot, I’ve chosen to organize the project by features: Customers, Orders, Settings, etc. Caliburn.Micro Soup to Nuts Part 7 - All About Conventions. One of the main features of Caliburn.Micro is manifest in its ability to remove the need for boiler plate code by acting on a series of conventions.

Caliburn.Micro Soup to Nuts Part 7 - All About Conventions

Some people love conventions and some hate them. That’s why CM’s conventions are fully customizable and can even be turned off completely if not desired. If you are going to use conventions, and since they are ON by default, it’s good to know what those conventions are and how they work. Caliburn.Micro Soup to Nuts Part 8–The EventAggregator. In Caliburn.Micro we have a series of supporting services for building presentation tiers.

Caliburn.Micro Soup to Nuts Part 8–The EventAggregator

Among them is the EventAggregator, a service which supports in-process publish/subscribe. There are various implementations of this pattern available in other frameworks, but I think you’ll find that Caliburn.Micro’s implementation sports the cleanest API and the richest set of features. Let’s start by having a look at the IEventAggregator interface: public interface IEventAggregator { void Subscribe(object instance); void Unsubscribe(object instance); void Publish(object message, Action<System.Action> marshal = null); } public interface IEventAggregator { void Subscribe(object instance); void Unsubscribe(object instance); void Publish(object message, Action<System.Action> marshal = null); } Below we’ll dig into how the default implementation of this interface (EventAggregator) works.

Subscribe. Caliburn.Micro Soup to Nuts Part 9–New WP7 Features. In version 1.0 we had pretty good support for building apps for WP7, but in v1.1 we’ve taken things up a notch. Let’s look at the same HelloWP7 sample that we did previously, but see how it’s been updated to take advantage of our improved tombstoning, launcher/chooser support and strongly typed navigation. Caliburn Micro: WPF, Silverlight and WP7 made easy.