.NET & Funky Fresh

Syndication

News

  • <script type="text/javascript" src="http://ws.amazon.com/widgets/q?ServiceVersion=20070822&amp;MarketPlace=US&amp;ID=V20070822/US/bluspiconinc-20/8001/8b68bf4b-6724-40e7-99a5-a6decf6d8648"> </script>
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. Let’s start by getting the framework.  Head on over to http://caliburnmicro.codeplex.com/SourceControl/list/changesets  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. Open the “Caliburn.Micro.sln” Pess Ctrl-Shift-B (or use the Build menu) to build the solution in Visual Studio. Now that we have the framework and it’s been successfully built, let’s create a simple application.

Open Visual Studio and create a new Silverlight 4 Application called “Caliburn.Micro.Hello”. You don’t need a web site or test project. Add a reference to System.Windows.Interactivity.dll and Caliburn.Micro.dll. You can find them both in the \src\Caliburn.Micro.Silverlight\Bin\Release (or Debug) folder. Delete “MainPage.xaml” and clean up your “App.xaml.cs” so that it looks like this:

namespace Caliburn.Micro.Hello
{
    using System.Windows;

    public partial class App : Application
    {
        public App()
        {
            InitializeComponent();
        }
    }
}

Since Caliburn.Micro prefers a View-Model-First approach, let’s start there. Create your first VM and call it ShellViewModel. Use the following code for the implementation:

namespace Caliburn.Micro.Hello
{
    using System.Windows;

    public class ShellViewModel : PropertyChangedBase
    {
        string name;

        public string Name
        {
            get { return name; }
            set
            {
                name = value;
                NotifyOfPropertyChange(() => Name);
                NotifyOfPropertyChange(() => CanSayHello);
            }
        }

        public bool CanSayHello
        {
            get { return !string.IsNullOrWhiteSpace(Name); }
        }

        public void SayHello()
        {
            MessageBox.Show(string.Format("Hello {0}!", Name)); //Don't do this.
        }
    }
}

Notice that the ShellViewModel inherits from PropertyChangedBase. This is a base class that implements the infrastructure for property change notification and automatically performs UI thread marshalling. It will come in handy :)

Now that we have our VM, let’s create the bootstrapper that will configure the framework and tell it what to do. Create a new class named HelloBootstrapper. You can use this tiny bit of code:

namespace Caliburn.Micro.Hello
{
    public class HelloBootstrapper : Bootstrapper<ShellViewModel> {}
}

There are two Bootsrappers available in Caliburn.Micro. This version allows you to specify the type of “root view model” via the generic type. The “root view mdoel” is a VM that Caliburn.Micro will instantiate and use to show your application. Next, we need to place the HelloBootstrapper somewhere where it will be run at startup. To do that, change your App.xaml to match this:

<Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:Caliburn.Micro.Hello"
             x:Class="Caliburn.Micro.Hello.App">
    <Application.Resources>
        <local:HelloBootstrapper x:Key="bootstrapper" />
    </Application.Resources>
</Application>

All we have to do is place a Caliburn.Micro bootstrapper in the Application.Resources and it will do the rest of the work.* Now, run the application. You should see something like this:

ViewNotFound

Caliburn.Micro creates the ShellViewModel, but doesn’t know how to render it. So, let’s create a view. Create a new Silverlight User Control named ShellView. Use the following xaml:

<UserControl x:Class="Caliburn.Micro.Hello.ShellView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <StackPanel>
        <TextBox x:Name="Name" />
        <Button x:Name="SayHello"
                Content="Click Me" />
    </StackPanel>
</UserControl>

Run the application again. You should now see the UI:

ViewFound

Typing something in the TextBox will enable the Button and clicking it will show a message:

ViweWithData

Caliburn.Micro uses a simple naming convention to locate Views for ViewModels.  Essentially, it takes the FullName and removes “Model” from it. So, given MyApp.ViewModels.MyViewModel, it would look for MyApp.Views.MyView. Looking at the View and ViewModel side-by-side, you can see that the TextBox with x:Name=”Name” is bound to the “Name” property on the VM. You can also see that the Button with x:Name=”SayHello” is bound to the method with the same name on the VM.  The “CanSayHello” property is guarding access to the “SayHello” action by disabling the Button. These are the basics of Caliburn.Micro’s ActionMessage and Conventions functionality. There’s much more to show. But, next time I want to show how we can integrate an IoC container such as MEF. This sample is attached below.

 

 

*All the functionality described in this article works identically for both Caliburn.Micro and Caliburn…with one exception. Caliburn does not have a Bootstrapper currently. At this time, you would inherit from CaliburnApplication and override CreateRootModel. In the coming months, Caliburn’s custom application class will be replaced by a bootstrapper mechanism similar to Caliburn.Micro.


Posted 07-06-2010 1:47 PM by Rob Eisenberg

[Advertisement]

Comments

Matt Hidinger wrote re: Caliburn.Micro Soup to Nuts Pt. 1 – Configuration, Actions and Conventions
on 07-06-2010 2:04 PM

Thanks Rob... Really looking forward to the rest of this series. I've been pretty hooked on some of your ideas since your MIX talk. I just wish I had been using Silverlight more prior to the last 3 months.

-Matt

John Kattestaart wrote re: Caliburn.Micro Soup to Nuts Pt. 1 – Configuration, Actions and Conventions
on 07-06-2010 3:07 PM

Can't wait for the next parts.

Luke Foust wrote re: Caliburn.Micro Soup to Nuts Pt. 1 – Configuration, Actions and Conventions
on 07-06-2010 3:31 PM

Great intro. I am looking forward to the rest of this series.

Nigel Sampson wrote re: Caliburn.Micro Soup to Nuts Pt. 1 – Configuration, Actions and Conventions
on 07-06-2010 5:00 PM

Am I correct in assuming that we can't use parameters to methods like SayHello in the WP7 build due to lack of binding infrastructure?

mthornal wrote re: Caliburn.Micro Soup to Nuts Pt. 1 – Configuration, Actions and Conventions
on 07-06-2010 5:02 PM

Thanks for this. The tutorials are so important in encouraging uptake. Keep up the great work.

Rob Eisenberg wrote re: Caliburn.Micro Soup to Nuts Pt. 1 – Configuration, Actions and Conventions
on 07-06-2010 5:12 PM

@Nigel

You are correct. For WP7, you will only be able to use parameters that are literals or "special values." Parameters that rely on properties of other elements in the tree will not work due to the incomplete DO/DP implementation in WP7. I'll discuss parameters in detail in Part 3.

Simon wrote re: Caliburn.Micro Soup to Nuts Pt. 1 – Configuration, Actions and Conventions
on 07-07-2010 9:05 AM

Is there anything different required to get this to work in vanilla WPF4? I have followed the example, except creating a Window except for a User Control, but nothing shows on screen. Thanks.

Rob Eisenberg wrote re: Caliburn.Micro Soup to Nuts Pt. 1 – Configuration, Actions and Conventions
on 07-07-2010 9:24 AM

@Simon

Apparently there is a difference in how SIlverlight and WPF ResourceDictionaries work. Try using this for your App.xaml:

<Application xmlns="schemas.microsoft.com/.../presentation"

            xmlns:x="schemas.microsoft.com/.../xaml"

            xmlns:local="clr-namespace:Caliburn.Micro.Hello"

            x:Class="Caliburn.Micro.Hello.App">

   <Application.Resources>

       <ResourceDictionary>

           <ResourceDictionary.MergedDictionaries>

               <ResourceDictionary>

                   <local:HelloBootstrapper x:Key="bootstrapper" />

               </ResourceDictionary>

           </ResourceDictionary.MergedDictionaries>

       </ResourceDictionary>

   </Application.Resources>

</Application>

SondreB wrote re: Caliburn.Micro Soup to Nuts Pt. 1 – Configuration, Actions and Conventions
on 07-07-2010 9:46 AM

As Simon mentions, this example doesn't seem to work with WPF. Bootstrapper instance is never created, might appear as though Application.Resources works differently on SL and WPF?

Bas ter Vrugt wrote re: Caliburn.Micro Soup to Nuts Pt. 1 – Configuration, Actions and Conventions
on 07-07-2010 9:47 AM

Nice article! looking forward to the upcoming articles

Simon wrote re: Caliburn.Micro Soup to Nuts Pt. 1 – Configuration, Actions and Conventions
on 07-07-2010 10:06 AM

Thanks Rob - that works just great for me now.

Looking forward to the rest of the series. Planning on using this for a small app, so keep it up.

DaRage wrote re: Caliburn.Micro Soup to Nuts Pt. 1 – Configuration, Actions and Conventions
on 07-07-2010 4:40 PM

Is Caliburn.Micro to Caliburn like Firefox to Mozilla? i.e. will it end up replacing it?

.NET & Funky Fresh wrote Caliburn.Micro Soup to Nuts Pt. 2 – Customizing The Bootstrapper
on 07-08-2010 8:29 PM

In the last part we discussed the most basic configuration for Caliburn.Micro and demonstrated a couple

.NET & Funky Fresh wrote Caliburn.Micro Soup to Nuts Pt. 3 – All About Actions
on 07-17-2010 12:06 PM

We briefly introduced actions in Pt. 1 , but there is so much more to know. To begin our investigation

Jay R. Wren wrote re: Caliburn.Micro Soup to Nuts Pt. 1 – Configuration, Actions and Conventions
on 07-21-2010 9:53 PM

Any explanation WTF is going on with the Resource Dictionary initialization in WPF there? it makes no sense to me.

Simon Woods wrote re: Caliburn.Micro Soup to Nuts Pt. 1 – Configuration, Actions and Conventions
on 07-22-2010 9:10 AM

For WPF it appears that you also need to set the x:Name attribute in the application xaml

see

connect.microsoft.com/.../app-initializecomponent-is-not-generated-when-startupuri-and-startup-are-missing

.NET & Funky Fresh wrote Caliburn.Micro Soup to Nuts Pt. 4 – Working with Windows Phone 7
on 08-07-2010 10:43 PM

Hopefully, previous articles have you up to speed on what Caliburn.Micro is , its basic configuration

Community Blogs wrote Caliburn.Micro Soup to Nuts Pt. 4 – Working with Windows Phone 7
on 08-07-2010 10:53 PM

Hopefully, previous articles have you up to speed on what Caliburn.Micro is , its basic configuration

About The CodeBetter.Com Blog Network
CodeBetter.Com FAQ

Our Mission

Advertisers should contact Brendan

Subscribe
Google Reader or Homepage

del.icio.us CodeBetter.com Latest Items
Add to My Yahoo!
Subscribe with Bloglines
Subscribe in NewsGator Online
Subscribe with myFeedster
Add to My AOL
Furl CodeBetter.com Latest Items
Subscribe in Rojo

Member Projects
DimeCasts.Net - Derik Whittaker

Friends of Devlicio.us
Red-Gate Tools For SQL and .NET

NDepend

SlickEdit
 
SmartInspect .NET Logging
NGEDIT: ViEmu and Codekana
LiteAccounting.Com
DevExpress
Fixx
NHibernate Profiler
Unfuddle
Balsamiq Mockups
Scrumy
JetBrains - ReSharper
Umbraco
NServiceBus
RavenDb
Web Sequence Diagrams
Ducksboard<-- NEW Friend!

 



Site Copyright © 2007 CodeBetter.Com
Content Copyright Individual Bloggers

 

Community Server (Commercial Edition)