Jaroslaw Matlak Jaroslaw Matlak - 11 days ago 13
C# Question

Bootstrapper DisplayRootFor<MV>() Exception: No parameterless constructor

I need to create a simple application, where in

MainViewModel
something happens during initialization.

When I try to run my code, the exception occurs on line
DisplayRootViewFor<MainViewModel>();
in
AppBootstrapper
:


An exception of type 'System.MissingMethodException' occurred in mscorlib.dll but was not handled in user code

Additional information: No parameterless constructor defined for this object.


Bootstrapper:

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


MainViewModel:

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


MainView:

<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>


How can I get rid of this exception?

EDIT:

Changed acces modifier of
MainViewModel
to
public

Answer

Your 'MainViewModel' class and constructor have no access specifier so it defaults to 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
            }
        }
    }
Comments