An instance of the ViewModel can't be created

Topics: Prism v4 - Silverlight 4
Jul 3, 2012 at 2:44 PM
Edited Jul 3, 2012 at 2:53 PM

I'm programming a View with it's ViewModel. The View creates an instance of the ViewModel from xaml.

It just doesn't compile.

This particular project is a remake of the UICompositionQuickStart but with a few changes, RIA Services (Northwind Database), ViewModels declared from xaml, and some others. Just to resemble one of my projects.

a

b

 Here is the ViewModel:

public class EmployeeListViewModel : INotifyPropertyChanged {
    private readonly IEventAggregator eventAggregator;

    public EmployeeListViewModel() {
        this.eventAggregator = ServiceLocator.Current.GetInstance<IEventAggregator>();
        this.Employees.CurrentChanged += new EventHandler(this.SelectedEmployeeChanged);
    }

    public ICollectionView Employees {
        get;
        private set;
    }

    private void SelectedEmployeeChanged(object sender, EventArgs e) {
        Employee employee = this.Employees.CurrentItem as Employee;
        if (employee != null) {
            // Publish the EmployeeSelectedEvent event.
            this.eventAggregator.GetEvent<EmployeeSelectedEvent>().Publish(employee);
        }
    }

    #region INotifyPropertyChanged Members

    public event PropertyChangedEventHandler PropertyChanged;

    private void NotifyPropertyChanged(string propertyName) {
        if (this.PropertyChanged != null) {
            this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    #endregion
}

Apparently the problem is creating the new EventHandler...

Jul 3, 2012 at 3:06 PM
Edited Jul 3, 2012 at 3:06 PM

I solved the problem using DesignerProperties in the ViewModel's constructor:

public EmployeeListViewModel() {
    if (!DesignerProperties.IsInDesignTool) {
        this.eventAggregator = ServiceLocator.Current.GetInstance<IEventAggregator>();
        this.Employees.CurrentChanged += new EventHandler(this.SelectedEmployeeChanged);
    }
}