View model instantiated more than once. (PRISM)

470 views Asked by At

I have a Main View with a toolbar and a TabControl region that has two views registered: View A, View B. All views should have as DataContext the same instance of ContactsViewModel, but in fact, each view is creating a new instance of ContactsViewModel.

This is the Main view code-behind:

public partial class ContactsView : UserControl
{
    public IRegionManager regionManager;

    private static Uri listViewUri = new Uri("/ContactsListView", UriKind.Relative);
    private static Uri tilesViewUri = new Uri("/ContactsTilesView", UriKind.Relative);

    public ContactsView(ContactsViewModel contactsViewModel, IRegionManager regionManager, IUnityContainer container)
    {
        this.ViewModel = contactsViewModel;
        container.RegisterType<ContactsViewModel>();
        this.regionManager = regionManager;
        InitializeComponent();
    }

    public ContactsViewModel ViewModel
    {
        get { return this.DataContext as ContactsViewModel; }
        set { this.DataContext = value; }
    }
}

This is the view A code-behind:

public partial class ContactsListView : UserControl
{
    public ContactsListView(IUnityContainer container)
    {
        ContactsViewModel viewModel = container.Resolve<ContactsViewModel>();
        this.ViewModel = viewModel;
        InitializeComponent();

        SetupColumns();
    }

    public ContactsViewModel ViewModel
    {
        get { return this.DataContext as ContactsViewModel; }
        set { this.DataContext = value; }
    }
}

View B is similar to View A.

And this is the ViewModel:

public class ContactsViewModel : BindableBase
{
    private readonly IRegionManager regionManager;
    private readonly IEventAggregator eventAggregator;
    private readonly IConfigurationContactsService contactsService;

    private readonly DelegateCommand<object> deleteContactCommand;

    private ObservableCollection<Contact> contactsCollection;
    private ICollectionView contactsView;

    public ContactsViewModel(IEventAggregator eventAggregator, IConfigurationContactsService contactsService, IRegionManager regionManager)
    {
        this.regionManager = regionManager;
        this.contactsService = contactsService;
        this.eventAggregator = eventAggregator;

        this.deleteContactCommand = new DelegateCommand<object>(this.DeleteContact, this.CanDeleteContact);

        this.contactsCollection = new ObservableCollection<Contact>(contactsService.GetContacts());
        this.contactsView = CollectionViewSource.GetDefaultView(this.contactsCollection);
    }

    public ICollectionView ContactsView
    {
        get { return this.contactsView; }
    }
    public ObservableCollection<Contact> Contacts
    {
        get { return this.contactsCollection; }
    }

    public ICommand DeleteContactCommand
    {
        get { return this.deleteContactCommand; }
    }

    private void DeleteContact(object ignore)
    {
        IList<Contact> selectedContacts = contactsService.GetSelectedContacts();
        foreach (Contact contact in selectedContacts)
        {
            if (contact != null)
            {
                contactsService.DeleteContact(contact);
            }
        }
        SetProperty<ObservableCollection<Contact>>(ref this.contactsCollection, new ObservableCollection<Contact>(contactsService.GetContacts()), "Contacts");
    }
    private bool CanDeleteContact(object ignored)
    {
        return contactsService.GetSelectedContacts().Any();
    }

}

How can I do ContactsListView (here called View A) to have the same instance of ContactsViewModel than the MainView?

EDITTED

Code in Main View and View A editted so in Main View I register the ViewModel into the container and in View A I Resolve the viewmodel. Still getting three instances. When the view model is resolved, a new instance is created.

1

There are 1 answers

0
chincheta73 On

As Richards suggested, I fixed the issue by registering the viewmodel as a singleton:

container.RegisterInstance<ContactsViewModel>(contactsViewModel);