视图模型不止一次实例化。 (PRISM)

时间:2017-01-02 12:07:25

标签: mvvm view prism viewmodel viewmodellocator

我有一个带有工具栏的主视图和一个注册了两个视图的TabControl区域:视图A,视图B.所有视图都应该具有与ContactViewModel相同的DataContext实例,但事实上,每个视图都在创建一个新实例of ContactsViewModel。

这是主视图代码隐藏:

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; }
    }
}

这是一个代码隐藏的视图:

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; }
    }
}

视图B类似于视图A.

这是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();
    }

}

如何使ContactsListView(此处称为View A)具有与MainView相同的ContactsViewModel实例?

EDITTED

主视图和视图中的代码在主视图中进行编辑我将ViewModel注册到容器中,并在View A I Resolve the viewmodel中注册。还有三个实例。解析视图模型后,将创建一个新实例。

1 个答案:

答案 0 :(得分:0)

正如Richards建议的那样,我通过将viewmodel注册为单身人来解决了这个问题:

container.RegisterInstance<ContactsViewModel>(contactsViewModel);