Ideablade Cocktail for Windows Store – Part 1

Ideablade is currently offering licenses to Windows Store version for free. The Dev_Tour sample shows how a basic DevForce is set up. However, there is scant documentation on how to create a Windows Store application that runs on top of the Ideablade Cocktail framework.

There are two Windows Store samples bundled with Cocktail – the Todo and the NavSample. The best place to start is the NavSample project. The Todo sample is a little too simplistic to learn much from.

Create Windows Store Application

Create a blank Windows Store Application using the Grid app as a base, and we will call it AssignmentTracker.RT and the solution is named sans the RT suffix. Assignment Tracker allows teachers to assign homework to students. It is not very different from a Todo list conceptually. The reason we add RT to the project is because, one day, we might wish to develop a desktop version.

However, we don’t want the created assemblies to have the .RT suffix. So, go into Project Properties and set accordingly.

Add packages via NuGet

After you create a blank Windows Store application in Visual Studio, add the following packages via NuGet:

  1. IdeaBlade.Cocktail
  2. IdeaBlade.Cocktail.Utils
  3. IdeaBlade.DevForce.Core
  4. Microsoft.Composition

Bootstrapper

The first thing that strikes any seasoned Caliburn.Micro user is that there is no Bootstrapper class in Caliburn.Micro.RT. This is because the Windows Store application provides a base Application class that you must override to get events. I was somewhat dismayed at Microsoft for doing this. WPF, Silverlight and Windows Phone all had one startup model and WinRT comes along and insists on another.

On the other hand, the people at Caliburn.Micro had done a pretty decent job of abstracting over this. There are two base classes provided – CocktailMefWindowsStoreApplication and CocktailWindowsStoreApplication, and they behave in the same way as the bootstrapper, all the normal overrides like Configure, SelectAssemblies, StartRuntime et c are still there, so you retain instant familiarity. (Don’t worry if you don’t know much about Caliburn.Micro, under the hood Cocktail uses a lot of Caliburn.Micro, but Cocktail has a smaller API surface area).

Once NuGet packages are installed, go to App.xaml.cs delete the entire application class (and enjoy the instant weight reduction!) and replace it with this. (Replace NavSample with your own project name)

<cocktail:CocktailMefWindowsStoreApplication
    x:Class="AssignmentTracker.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:cocktail="using:Cocktail"
    xmlns:local="using:AssignmentTracker"
    xmlns:localData="using:AssignmentTracker.RT.Data">

    <cocktail:CocktailMefWindowsStoreApplication.Resources>

        <!-- Application-specific resources -->

        <x:String x:Key="AppName">AssignmentTracker</x:String>

    </cocktail:CocktailMefWindowsStoreApplication.Resources>
</cocktail:CocktailMefWindowsStoreApplication>
using Cocktail;
using IdeaBlade.Core;

namespace AssignmentTracker
{
    /// <summary>
    /// Provides application-specific behavior to supplement the default Application class.
    /// </summary>
    sealed partial class App : CocktailMefWindowsStoreApplication
    {
        /// <summary>
        /// Initializes the singleton Application object.  This is the first line of authored code
        /// executed, and as such is the logical equivalent of main() or WinMain().
        /// </summary>
        public App(): base(typeof(GroupedItemsPageViewModel))
        {
            this.InitializeComponent();
        }

        protected override void StartRuntime()
        {
            base.StartRuntime();

            IdeaBladeConfig.Instance.ObjectServer.RemoteBaseUrl = "http://localhost";
            IdeaBladeConfig.Instance.ObjectServer.ServerPort = 57209;
            IdeaBladeConfig.Instance.ObjectServer.ServiceName = "EntityService.svc";
        }

    }
}

The GroupedItemsPageViewModel is the first ViewModel that will be loaded by Cocktail. Cocktail then automatically finds GroupedItemsPage.xaml or GroupedItemsPageView.xaml, and displays it on the screen. (This is known as ViewModel-first style. If you are careful, you can develop and test the ViewModels without any dependencies on the actual visual controls, and makes it easier to port to other platforms.)

Next: Defining GroupedItemsPageViewModel