我对C#编程很陌生。我开始使用MVVM模式和Caliburn.Micro框架开发WPF应用程序。我还添加了Caliburn.Micro.Contrib库,因为它包含一些有用的对话框。
我尝试在我的项目中使用它,但我无法让它工作。 当我点击应该显示对话框的按钮时,我有一个空窗口,说无法找到该视图:(Caliburn.Micro.Contrib.Dialogs.DialogViewModel`1 [Caliburn.Micro.Contrib.Dialogs.Answer])
我已经设置了Caliburn.Micro引导程序来嵌入CM Contrib程序集:
protected override IEnumerable<Assembly> SelectAssemblies()
{
return base.SelectAssemblies().Concat(new Assembly[] { typeof(ResultExtensions).Assembly });
}
我的视图模型类中的execute方法:
public IEnumerable<IResult> DeleteItem()
{
var question = new Question("The Subject","The Message", Answer.Yes, Answer.No);
yield return question.AsResult().CancelOnResponse(Answer.No);
}
提前感谢您的帮助。
修改:
这里是完整的bootstrap
public class NInjectBootstrapper<TRootModel> : Bootstrapper<TRootModel>
{
protected IKernel Kernel { get; private set; }
/// <summary>
/// The base type required for a view model
/// </summary>
public Type ViewModelBaseType { get; set; }
protected override void Configure()
{
// allow base classes to change bootstrapper settings
ConfigureBootstrapper();
var loadedAssemblies = AppDomain.CurrentDomain.GetAssemblies().ToList();
var loadedPaths = loadedAssemblies.Select(a => a.Location).ToArray();
var referencedPaths = Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory, "*.dll");
var toLoad = referencedPaths.Where(r =>r.Contains("TxS.") && !loadedPaths.Contains(r, StringComparer.InvariantCultureIgnoreCase)).ToList();
toLoad.ForEach(path => loadedAssemblies.Add(AppDomain.CurrentDomain.Load(AssemblyName.GetAssemblyName(path))));
Bootstrapper.With.Ninject().Start();
Kernel = Bootstrapper.Container as IKernel;
//ViewLocator.AddNamespaceMapping("Caliburn.Micro.Contrib.Dialogs", "TxSGUI.Views");
}
protected override object GetInstance(System.Type service, string key)
{
object instance;
if (string.IsNullOrWhiteSpace(key))
{
instance = Kernel.TryGet(service);
}
else
{
instance = Kernel.TryGet(service, key);
}
if(instance == null)
throw new Exception(string.Format("Could not locate any instances of contract {0}", key ?? service.Name));
return instance;
}
protected override IEnumerable<object> GetAllInstances(System.Type service)
{
return Kernel.GetAll(service);
}
protected override void BuildUp(object instance)
{
Kernel.Inject(instance);
}
protected virtual void ConfigureBootstrapper()
{
// the default view model base type
ViewModelBaseType = typeof(System.ComponentModel.INotifyPropertyChanged);
}
protected override IEnumerable<Assembly> SelectAssemblies()
{
return base.SelectAssemblies().Concat(new Assembly[] { typeof(ResultExtensions).Assembly });
}
/// <summary>
/// Override to include your own Autofac configuration after the framework has finished its configuration, but
/// before the container is created.
/// </summary>
/// <param name="builder">The Autofac configuration builder.</param>
protected virtual void ConfigureContainer(IKernel builder)
{
}
}
和NInject模块(绑定视图和其他东西)
public class TxSGuiModule : NinjectModule
{
public override void Load()
{
// Bind Window Manager
Kernel.Bind<IWindowManager>().To<WindowManager>().InSingletonScope();
// Bind Event Aggregator
Kernel.Bind<IEventAggregator>().To<EventAggregator>().InSingletonScope();
// Bind Main View
Kernel.Bind<TxSMainViewModel>().To<TxSMainViewModel>().InSingletonScope().Named(typeof(TxSMainViewModel).Name);
// Bind Views
Kernel.Bind(x => x.FromThisAssembly()
.SelectAllClasses().InheritedFrom<IShell>()
.Where(t => t.Name.EndsWith("ViewModel") && t.Namespace.StartsWith("TxSGUI"))
.BindSelection((a, c) =>
{
IList<Type> list = new List<Type>();
list.Add(typeof(IShell));
list.Add(a);
return list;
})
.Configure(b => b.InTransientScope()));
// Bind Actions
Kernel.Bind(x => x.FromThisAssembly().SelectAllClasses().InheritedFrom<IAction>().BindAllInterfaces().Configure(b => b.InSingletonScope()));
}
}
激活Caliburn Log后,我在这里:
07/31/2013 14:46:21 - INFO - [Coroutine] - Executing coroutine.
07/31/2013 14:46:21 - WARN - [ViewLocator] - View not found. Searched: Caliburn.Micro.Contrib.Dialogs.Dialog.Question.
07/31/2013 14:46:21 - INFO - [ViewModelBinder] - Binding System.Windows.Window: Cannot find view for Caliburn.Micro.Contrib.Dialogs.DialogViewModel`1[Caliburn.Micro.Contrib.Dialogs.Answer]. and Caliburn.Micro.Contrib.Dialogs.DialogViewModel`1[Caliburn.Micro.Contrib.Dialogs.Answer].
07/31/2013 14:46:21 - INFO - [Action] - Setting DC of System.Windows.Window: Cannot find view for Caliburn.Micro.Contrib.Dialogs.DialogViewModel`1[Caliburn.Micro.Contrib.Dialogs.Answer]. to Caliburn.Micro.Contrib.Dialogs.DialogViewModel`1[Caliburn.Micro.Contrib.Dialogs.Answer].
07/31/2013 14:46:21 - INFO - [Action] - Attaching message handler Caliburn.Micro.Contrib.Dialogs.DialogViewModel`1[Caliburn.Micro.Contrib.Dialogs.Answer] to System.Windows.Window: Cannot find view for Caliburn.Micro.Contrib.Dialogs.DialogViewModel`1[Caliburn.Micro.Contrib.Dialogs.Answer]..
07/31/2013 14:46:21 - INFO - [ViewModelBinder] - Attaching System.Windows.Window: Cannot find view for Caliburn.Micro.Contrib.Dialogs.DialogViewModel`1[Caliburn.Micro.Contrib.Dialogs.Answer]. to Caliburn.Micro.Contrib.Dialogs.DialogViewModel`1[Caliburn.Micro.Contrib.Dialogs.Answer].