TheBlueSky
TheBlueSky

Reputation: 5948

Can't Make Caliburn.Micro Work With Windows Phone

I'm trying to understand how Caliburn.Micro works with Windows Phone (and MVVM in general) so I created a basic Windows Phone Application, installed Caliburn.Micro NuGet package (v1.2.0 - the latest for now) and followed the few instructions here and there. So, I ended up with:

WMAppManifest.xml

<DefaultTask  Name ="_default" NavigationPage="Views/HomeView.xaml"/>

Framework/AppBootstrapper.cs

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Windows;
using Caliburn.Micro;
using MyCaliburn.PhoneUI.ViewModels;

namespace MyCaliburn.PhoneUI.Framework
{
    public class AppBootstrapper : PhoneBootstrapper
    {
        PhoneContainer container;

        protected override void Configure()
        {
            container = new PhoneContainer(RootFrame);
            container.RegisterPhoneServices();
            container.Singleton<HomeViewModel>();
        }

        protected override void OnUnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
        {
            if (Debugger.IsAttached)
            {
                Debugger.Break();
                e.Handled = true;
            }
            else
            {
                MessageBox.Show("An unexpected error occured, sorry about the troubles.", "Oops...", MessageBoxButton.OK);
                e.Handled = true;
            }

            base.OnUnhandledException(sender, e);
        }

        protected override object GetInstance(Type service, string key)
        {
            return container.GetInstance(service, key);
        }

        protected override IEnumerable<object> GetAllInstances(Type service)
        {
            return container.GetAllInstances(service);
        }

        protected override void BuildUp(object instance)
        {
            container.BuildUp(instance);
        }
    }
}

ViewModels/HomeViewModel.cs

using Caliburn.Micro;

namespace MyCaliburn.PhoneUI.ViewModels
{
    public class HomeViewModel : Screen
    {
        public HomeViewModel()
        {
            //DisplayName = "Home";
        }
    }
}

View/HomeView.xaml.cs (the XAML page is the default Window Phone Portrait Page)

using Microsoft.Phone.Controls;

namespace MyCaliburn.PhoneUI.Views
{
    public partial class HomeView : PhoneApplicationPage
    {
        public HomeView()
        {
            InitializeComponent();
        }
    }
}

App.xaml

<Application
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="MyCaliburn.PhoneUI.App"
    xmlns:Framework="clr-namespace:MyCaliburn.PhoneUI.Framework">

    <!--Application Resources-->
    <Application.Resources>
        <Framework:AppBootstrapper x:Key="bootstrapper" />
    </Application.Resources>

</Application>

App.xaml.cs

using System.Windows;

namespace MyCaliburn.PhoneUI
{
    public partial class App : Application
    {
        /// <summary>
        /// Constructor for the Application object.
        /// </summary>
        public App()
        {
            // Standard Silverlight initialization
            InitializeComponent();
        }
    }
}

Now, when I hit F5, the application runs and exits without showing any page or exception and doesn't hit any breakpoints that I sit.

Can anyone tells me what's missing in my code which prevents the application from running?

Thanks in advance.

Upvotes: 0

Views: 461

Answers (1)

Filip Skakun
Filip Skakun

Reputation: 31724

Many times when I end up with an app that does not start - it turns out that due to some refactoring the App class is not the startup object any more. Right-click on the project in solution explorer, go to properties/Application and make sure Startup object is set correctly.

Upvotes: 3

Related Questions