带有Datacontext的WPF Ninject和带有参数的构造函数

时间:2018-07-02 08:43:20

标签: c# wpf xaml dependency-injection ninject

早上好,             我有一些使用WPF的经验,但是我仍然刚开始使用MVVM,现在我想将DI与ninject一起使用。当我使用表单时,通常通过使内核带有绑定来加载并在构造函数中插入参数来正常使用ninject,但由于必须使用DataContext,因此我看不到这是不可能的。 我在View Models中有一个带按钮的主窗口和一个构造器,为了在窗口中使用按钮,我需要传递构造函数中绑定的参数,但是如果这样做,我会收到错误消息,因为datacontext想要一个无参数的构造函数,我相信有一个显而易见的解决方案,我无法在Ninject(站点)上找到一个特定的解决方案,并且在网上,唯一的解决方法是使用一些复杂的工厂模式,这种模式似乎过大了,有人知道如何解决问题吗? / p>

MainWindow.xaml

<Window x:Class="StretchApp.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

        xmlns:local="clr-namespace:StretchApp"
        xmlns:vm="clr-namespace:StretchApp.ViewModels"
        mc:Ignorable="d"
        Title="MainWindow" Height="800" Width="800" Background="LightGray"
    DataContextChanged="MainWindow_DataContextChanged">
    <Window.DataContext>
        <vm:MainViewModel/>
    </Window.DataContext>

MainWindow.xaml.cs

namespace StretchApp
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window, IHavePassword
    {
        private IKernel container;

        public MainWindow()
        {
            InitializeComponent();
            DataContextChanged += new DependencyPropertyChangedEventHandler(MainWindow_DataContextChanged);
        }
        void MainWindow_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            var dc = DataContext as IClosableViewModel;
            dc.CloseWindowEvent += new EventHandler(dc_CloseWindowEvent);
        }
        void dc_CloseWindowEvent(object sender, EventArgs e)
        {
            this.Close();
        }
        private void ConfigureContainer()
        {
            var kernel = new StandardKernel();
            kernel.Load(Assembly.GetExecutingAssembly());
        }

    }
}

App.xaml

<Application x:Class="StretchApp.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:StretchApp"
             xmlns:local1="clr-namespace:StretchApp.ViewModels">

    <Application.Resources>
    </Application.Resources>
</Application>

App.xaml.xs

sing Ninject;
using StretchApp.Helper;
using StretchApp.Helper.Interfaces;
using System.Windows;

namespace StretchApp
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        private IKernel container;

        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);
            var container = new StandardKernel();

            // Register types
            container.Bind<IEncrypterHelper>().To<EncrypterHelper>();
            container.Bind<Window>().To<MainWindow>();

            // Show the main window.
            MainWindow mw = new MainWindow();
            mw.Show();

        }
        private void ComposeObjects()
        {
            Current.MainWindow = this.container.Get<MainWindow>();
        }

        private void ConfigureContainer()
        {
            this.container = new StandardKernel();
            container.Bind<IEncrypterHelper>().To<EncrypterHelper>();

        }
    }
}

MainViewModel.cs

        public MainViewModel(IEncrypterHelper encrypterHelper)
        {
           _encrypterHelper = encrypterHelper;

        }

1 个答案:

答案 0 :(得分:2)

不要像这样在XAML标记中实例化视图模型:

<Window.DataContext>
    <vm:MainViewModel/>
</Window.DataContext>

您只会在非常简单的情况下执行此操作,并且如果要注入具有某些依赖项的MainViewModel,则无法使用此方法。相反,您可以通过编程方式设置视图的DataContext属性,例如,在您的OnStartup方法中实例化MainWindow

protected override void OnStartup(StartupEventArgs e)
{
    base.OnStartup(e);
    var container = new StandardKernel();

    // Register types
    container.Bind<IEncrypterHelper>().To<EncrypterHelper>();
    container.Bind<Window>().To<MainWindow>();

    // Show the main window.
    MainWindow mw = new MainWindow();
    mw.DataContext = container.Get<MainWindowViewModel>();
    mw.Show();
}