我一直在学习MVVM/WPF,并已完成here教程。

我已经使用这种方法创建了一个可运行的应用程序,但是现在在一个新项目中,我无法使依赖项注入(inject)正常工作。

当我运行该项目时,我得到一个空的MainWindow,而没有注入(inject)CompanyView。我已经一遍又三遍地检查了在可行项目与该无效项目之间的所有内容,并且无法找到未注入(inject)CompanyView的原因。我也曾尝试清洁解决方案并重新启动VS无济于事。希望有人能看到我所缺少的。

我有以下文件:

App.xaml.cs (使用base.OnStartup()代替App.xaml中的StartupUri)

namespace SidekickAdmin
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {

        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);
            MainWindow window = new MainWindow();
            var viewModel = new MainWindowViewModel();
            window.DataContext = viewModel;
            window.Show();
        }

    }
}

MainWindowViewModel.cs
namespace SidekickAdmin.ViewModel
{
    class MainWindowViewModel : ViewModelBase
    {
        public MainWindowViewModel()
        {
            CompanyViewModel companyViewModel = new CompanyViewModel(_repository);
            this.ViewModels.Add(companyViewModel);

        }

        ObservableCollection<ViewModelBase> _viewModels;
        ObservableCollection<ViewModelBase> ViewModels
        {
            get
            {
                if (_viewModels == null)
                {
                    _viewModels = new ObservableCollection<ViewModelBase>();
                }
                return _viewModels;
            }
        }
    }
}

MainWindowView.xaml
<Window x:Class="SidekickAdmin.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:vm="clr-namespace:SidekickAdmin.ViewModel"
        xmlns:vw="clr-namespace:SidekickAdmin.View"
        Title="Sidekick Admin" SizeToContent="WidthAndHeight">

    <!-- Typically done in a resources dictionary -->
    <Window.Resources>
        <ResourceDictionary Source="MainWindowResources.xaml" />
    </Window.Resources>


    <StackPanel>
        <ItemsControl ItemsSource="{Binding ViewModel}" Margin="3" />
    </StackPanel>

</Window>

MainWindowResources.xaml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:vm="clr-namespace:SidekickAdmin.ViewModel"
                    xmlns:vw="clr-namespace:SidekickAdmin.View">

    <DataTemplate DataType="{x:Type vm:CompanyViewModel}">
        <vw:CompanyView />
    </DataTemplate>

</ResourceDictionary>

CompanyViewModel.cs (尚未真正使用,因为我仍在尝试仅使 View 显示)
namespace SidekickAdmin.ViewModel
{

    class CompanyViewModel : ViewModelBase
    {
        readonly GenericRepository _repository;

        #region Getters & Setters
        public ObservableCollection<Company> AllCompanies
        {
            get;
            private set;
        }

        #endregion

        #region Constructors
        public CompanyViewModel(GenericRepository repository)
        {

            if (repository == null)
            {
                throw new ArgumentNullException("repository");
            }

            _repository = repository;

            this.AllCompanies = new ObservableCollection<Company>(_repository.GetAll<Company>());
        }

        #endregion


        protected override void OnDispose()
        {
            this.AllCompanies.Clear();
        }

    }
}

CompanyView.xaml
<UserControl x:Class="SidekickAdmin.View.CompanyView"
             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"
             mc:Ignorable="d"
             Height="300" Width="300">

    <StackPanel>
        <TextBlock>You say Hello</TextBlock>
        <TextBlock>And I say Goodbye</TextBlock>
        <TextBlock>Hello, Hello</TextBlock>
    </StackPanel>

 </UserControl>

最佳答案

除了@Robert Levy编写的内容之外,您所犯的错误是ViewModels属性是私有(private)的,将其设为公共(public),并且应该可以正常工作。

关于wpf - 依赖注入(inject)不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8825606/

10-17 00:12