我需要创建一个简单的应用程序,其中MainViewModel
在初始化期间发生了某些事情。
当我尝试运行我的代码时,异常发生在DisplayRootViewFor<MainViewModel>();
的{{1}}行:
mscorlib.dll中发生了'System.MissingMethodException'类型的异常但未在用户代码中处理
附加信息:没有为此对象定义无参数构造函数。
引导程序:
AppBootstrapper
MainViewModel:
using System;
using Ninject;
using Caliburn.Micro;
namespace ChartsDisplayer2016
{
public class AppBootstrapper : BootstrapperBase
{
private IKernel kernel;
public AppBootstrapper()
{
Initialize();
}
protected override void Configure()
{
kernel = new StandardKernel();
kernel.Load(AppDomain.CurrentDomain.GetAssemblies());
kernel.Bind<IEventAggregator>().To<EventAggregator>().InSingletonScope();
}
protected override void BuildUp(object instance)
{
kernel.Inject(instance);
}
protected override void OnStartup(object sender, System.Windows.StartupEventArgs e)
{
base.OnStartup(sender, e);
DisplayRootViewFor<MainViewModel>();
}
}
}
的MainView:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ChartsDisplayer2016.Core.Charts.ViewModels;
using Caliburn.Micro;
namespace ChartsDisplayer2016.Core.Main.ViewModels
{
public class MainViewModel: Conductor<Screen>.Collection.OneActive
{
private readonly IEventAggregator eventAggregator;
private readonly IWindowManager windowManager;
MainViewModel()
{
//something important;
}
MainViewModel(IEventAggregator eventAggregator,
IWindowManager windowManager)
{
this.eventAggregator = eventAggregator;
this.eventAggregator.Subscribe(this);
this.windowManager = windowManager;
//something important
}
}
}
如何摆脱这种异常?
编辑:
将<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
x:Class="ChartsDisplayer2016.Core.Main.Views.MainView"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
</Grid>
</UserControl>
的访问修饰符更改为MainViewModel
答案 0 :(得分:2)
您的MainViewModel
类和构造函数没有访问说明符,因此默认为private。
namespace ChartsDisplayer2016.Core.Main.ViewModels
{
public class MainViewModel: Conductor<Screen>.Collection.OneActive
{
private readonly IEventAggregator eventAggregator;
private readonly IWindowManager windowManager;
public MainViewModel()
{
//something important;
}
public MainViewModel(IEventAggregator eventAggregator,
IWindowManager windowManager)
{
this.eventAggregator = eventAggregator;
this.eventAggregator.Subscribe(this);
this.windowManager = windowManager;
//something important
}
}
}