Note that there are some explanatory texts on larger screens.

plurals
  1. PODependency Injection not working
    primarykey
    data
    text
    <p>I have been learning MVVM / WPF and have gone through the tutorial <a href="http://msdn.microsoft.com/en-us/magazine/dd419663.aspx" rel="nofollow">here</a>.</p> <p>I have created a working application using this methodology but now, on a new project, I cannot get the Dependency Injection to work.</p> <p>When I run this project I get an empty MainWindow without the CompanyView injected. I have double and tripled checked everything between the project that works and this one that doesn't and cannot find the reason for CompanyView not being injected. I have also tried cleaning the solution and restarting VS to no avail. Hopefully someone can see what I am missing.</p> <p>I have the following file:</p> <p><strong>App.xaml.cs</strong> (using base.OnStartup() instead of StartupUri in App.xaml)</p> <pre><code>namespace SidekickAdmin { /// &lt;summary&gt; /// Interaction logic for App.xaml /// &lt;/summary&gt; 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(); } } } </code></pre> <p><strong>MainWindowViewModel.cs</strong></p> <pre><code>namespace SidekickAdmin.ViewModel { class MainWindowViewModel : ViewModelBase { public MainWindowViewModel() { CompanyViewModel companyViewModel = new CompanyViewModel(_repository); this.ViewModels.Add(companyViewModel); } ObservableCollection&lt;ViewModelBase&gt; _viewModels; ObservableCollection&lt;ViewModelBase&gt; ViewModels { get { if (_viewModels == null) { _viewModels = new ObservableCollection&lt;ViewModelBase&gt;(); } return _viewModels; } } } } </code></pre> <p><strong>MainWindowView.xaml</strong></p> <pre><code>&lt;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"&gt; &lt;!-- Typically done in a resources dictionary --&gt; &lt;Window.Resources&gt; &lt;ResourceDictionary Source="MainWindowResources.xaml" /&gt; &lt;/Window.Resources&gt; &lt;StackPanel&gt; &lt;ItemsControl ItemsSource="{Binding ViewModel}" Margin="3" /&gt; &lt;/StackPanel&gt; &lt;/Window&gt; </code></pre> <p><strong>MainWindowResources.xaml</strong></p> <pre><code>&lt;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"&gt; &lt;DataTemplate DataType="{x:Type vm:CompanyViewModel}"&gt; &lt;vw:CompanyView /&gt; &lt;/DataTemplate&gt; &lt;/ResourceDictionary&gt; </code></pre> <p><strong>CompanyViewModel.cs</strong> (not really used yet as I am still trying to just get the view to appear)</p> <pre><code>namespace SidekickAdmin.ViewModel { class CompanyViewModel : ViewModelBase { readonly GenericRepository _repository; #region Getters &amp; Setters public ObservableCollection&lt;Company&gt; AllCompanies { get; private set; } #endregion #region Constructors public CompanyViewModel(GenericRepository repository) { if (repository == null) { throw new ArgumentNullException("repository"); } _repository = repository; this.AllCompanies = new ObservableCollection&lt;Company&gt;(_repository.GetAll&lt;Company&gt;()); } #endregion protected override void OnDispose() { this.AllCompanies.Clear(); } } } </code></pre> <p><strong>CompanyView.xaml</strong> </p> <pre><code>&lt;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"&gt; &lt;StackPanel&gt; &lt;TextBlock&gt;You say Hello&lt;/TextBlock&gt; &lt;TextBlock&gt;And I say Goodbye&lt;/TextBlock&gt; &lt;TextBlock&gt;Hello, Hello&lt;/TextBlock&gt; &lt;/StackPanel&gt; &lt;/UserControl&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload