Caliburn.Micro does not have “Bootstrapper” in a n

2019-06-11 11:53发布

问题:

EDIT: It seems that I finally made it to work. I asked an author of that post which I referred before and he said that it's a known issue. He also gave mi a workaround (in the comment below the post), so I consider this question as closed. But thanks to all of you for time spent with my problems :)


I'm trying to learn MVVM with Caliburn Micro framework, but I got problems from the beginning. I'm following this tutorial and I got such a code in App.xaml:

<Application
x:Class="Caliburn.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:caliburnMicro="clr-namespace:Caliburn">
<!--Application Resources-->
    <Application.Resources>
        <caliburnMicro:Bootstrapper x:Key="bootstrapper" />
    </Application.Resources>
</Application>

But I got an error:

The name "Bootstrapper" does not exist in the namespace "clr-namespace:Caliburn".

I got Caliburn Micro 1.5.2 from NuGet repository. Any ideas appreciated...

My Bootstrapper:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Caliburn.Micro;

namespace Caliburn
{
public class Bootstrapper : PhoneBootstrapper
{
    PhoneContainer container;

    protected override void Configure()
    {
        container = new PhoneContainer();

        container.RegisterPhoneServices(RootFrame);
        //container.PerRequest<MainPageViewModel>();

        AddCustomConventions();
    }

    static void AddCustomConventions()
    {
        //ellided  
    }

    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);
    }
}
}

回答1:

You should define your own Bootstrapper type which derives from one of the Caliburn.Micro bootstrapper types. The resource in your application resources should then be an instance of this bootstrapper.

The easiest option whilst learning is to use the Caliburn.Micro.Start NuGet package, and have a look at its bootstrapper implementation. The documentation also describes the markup you should use in your App.xaml file.



回答2:

I think everyone got confused because you named your own namespace caliburn so they so thought you are trying to create an instance of the framework's bootstrapper so i suggest to alter your naming conventions. With that out of our way, instead of the bootstrapper directly into the application resources, try putting it in a resource dictionary like this this:

<Application
x:Class="Caliburn.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:caliburnMicro="clr-namespace:Caliburn">
<!--Application Resources-->
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary>
                    <local:Bootstrapper x:Key="bootstrapper" />
                </ResourceDictionary>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>


回答3:

I had the same problem and it´s easy to solve. You need to override the method OnStart in your Bootstrapper class and in this method set which is your root view.

protected override void OnStartup(object sender, StartupEventArgs e)
{
    DisplayRootViewFor<ViewModels.ShellVM>();
}