我正在尝试使用Caliburn Micro框架学习MVVM,但我从一开始就遇到了问题。我正在关注this教程,我在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>
但我收到了一个错误:
名称“Bootstrapper”在命名空间“clr-namespace:Caliburn”中不存在。
我从NuGet存储库获得了Caliburn Micro 1.5.2。任何想法都赞赏......
我的引导程序:
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);
}
}
}
答案 0 :(得分:4)
您应该定义自己的Bootstrapper类型,该类型派生自Caliburn.Micro引导程序类型之一。然后,应用程序资源中的资源应该是此引导程序的实例。
学习时最简单的选择是使用Caliburn.Micro.Start NuGet package,并查看其引导程序实现。 documentation还描述了您应该在App.xaml文件中使用的标记。
答案 1 :(得分:1)
我认为每个人都感到困惑,因为你命名了自己的命名空间caliburn
所以他们认为你正在尝试创建框架引导程序的实例,所以我建议改变你的命名约定。借助我们的方式,而不是将引导程序直接放入应用程序资源中,尝试将它放在这样的资源字典中:
<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>
答案 2 :(得分:-1)
我遇到了同样的问题而且很容易解决。您需要在Bootstrapper类中以及在此方法集中覆盖方法OnStart,这是您的根视图。
protected override void OnStartup(object sender, StartupEventArgs e)
{
DisplayRootViewFor<ViewModels.ShellVM>();
}