service返回null wpf mvvm

时间:2015-10-05 07:22:50

标签: c# wpf mvvm ninject

我正在尝试在我的数据上下文中绑定数据,但服务返回null。我正在使用ninject作为DI。我正在分享我的app.xaml.cs代码。请指导我。

public partial class App
{
    private IKernel container;

    protected override void OnStartup(StartupEventArgs e)
    {
        base.OnStartup(e);
        AutoMapperConfig.RegisterMappings();
        ConfigureContainer();
        ComposeObjects();
        Current.MainWindow.Show();
    }

    private void ConfigureContainer()
    {
        this.container = new StandardKernel();
        container.Bind(typeof(IAppServiceBase<>)).To(typeof(AppServiceBase<>));
        container.Bind<IJvDetailAppService>().To<JvDetailAppService>().InSingletonScope();
        container.Bind<IJvMasterAppService>().To<JvMasterAppService>().InSingletonScope();
        container.Bind<IJvDimensionAppService>().To<JvDimensionAppService>().InSingletonScope();
        container.Bind(typeof(IServiceBase<>)).To(typeof(ServiceBase<>));
        container.Bind<IJvDetailService>().To<JvDetailService>();
        container.Bind<IJvMasterService>().To<JvMasterService>();
        container.Bind<IJvDimensionService>().To<JvDimensionService>();

        container.Bind(typeof(IRepositoryBase<>)).To(typeof(RepositoryBase<>));
        container.Bind<IJvDetailRepository>().To<JvDetailRepository>();
        container.Bind<IJvMasterRepository>().To<JvMasterRepository>();
        container.Bind<IJvDimensionRepository>().To<JvDimensionRepository>();
    }

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

MainWindowViewModel

public class MainWindowViewModel : ViewModelBase
{
    private readonly BackgroundWorker _worker = new BackgroundWorker();

    private ObservableCollection<JvDetailViewModelBase> _jvDetailMenu;
    private readonly IJvDetailAppService _jvDetailAppService;

    public ObservableCollection<JvDetailViewModelBase> JvDetailMenu
    {
        get { return this._jvDetailMenu; }
        set
        {
            _jvDetailMenu = value;
            RaisedPropertyChanged("JvDetailMenu");
        }
    }

    public MainWindowViewModel()
    {
        _worker.DoWork += worker_DoWork;
        _worker.RunWorkerCompleted += worker_RunWorkerCompleted;
        _worker.RunWorkerAsync();

    }

    readonly ObservableCollection<JvDetailViewModelBase> _tempProductMenu = new ObservableCollection<JvDetailViewModelBase>();
    private void worker_DoWork(object sender, DoWorkEventArgs e)
    {
      _tempProductMenu.Add(new ModifyJvDetailViewModel(_jvDetailAppService));
    }

    private void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        if (_tempProductMenu.Any())
        {
            JvDetailMenu = _tempProductMenu;
        }
    }
}

ModifyJvDetailViewModel.cs

public class ModifyJvDetailViewModel : JvDetailViewModelBase
{
    private readonly BackgroundWorker _worker = new BackgroundWorker();

    private IEnumerable<JvDetail> _tempLoadJvDetails;

    private readonly IJvDetailAppService _jvDetailAppService;

    private void worker_DoWork(object sender, DoWorkEventArgs e)
    {
        int ab = _jvDetailAppService.GetByCompanyId(3).Count(); // it is returning null here and everywhere which is fetching records.
        _tempLoadJvDetails = _jvDetailAppService.GetByCompanyId(3);
    }
    private void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        JvDetails = _tempLoadJvDetails;
    }
    public override string Name
    {
        get { return JustAppHelper.JvDetail; }
    }

    public override string Icon
    {
        get { return JustAppHelper.JvDetailIcon; }
    }


    public ModifyJvDetailViewModel(IJvDetailAppService jvDetailAppService)
    {
        _jvDetailAppService = jvDetailAppService;
        var ab = _jvDetailAppService.GetAll().Count();

        _worker.DoWork += worker_DoWork;
        _worker.RunWorkerCompleted += worker_RunWorkerCompleted;
        _worker.RunWorkerAsync();
        BindGrid();

    }

    protected void BindGrid()
    {
        JvDetails = _jvDetailAppService.GetByCompanyId(3);
    }

    private IEnumerable<JvDetail> _jvDetails;
    public IEnumerable<JvDetail> JvDetails
    {
        get { return _jvDetails; }
        set
        {
            _jvDetails = value;
            RaisedPropertyChanged("JvDetails");
        }
    }
}

Mainwindow.cs

public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = new MainWindowViewModel();
            ListBoxProducts.SelectedIndex = 0;
        }
    }

1 个答案:

答案 0 :(得分:0)

一种方法是更改​​MainWindowViewModel构造函数:

public MainWindowViewModel(IJvDetailAppService detailAppService)
{
    _jvDetailAppService = detailAppService;

    _worker.DoWork += worker_DoWork;
    _worker.RunWorkerCompleted += worker_RunWorkerCompleted;
    _worker.RunWorkerAsync();
}

MainWindow构造函数:

public MainWindow(IJvDetailAppService detailAppService)
{
    InitializeComponent();
    this.DataContext = new MainWindowViewModel(detailAppService);
    ListBoxProducts.SelectedIndex = 0;
}