0

我有一个带有工具栏的主视图和一个 TabControl 区域,该区域注册了两个视图:视图 A、视图 B。所有视图都应具有与 DataContext 相同的 ContactsViewModel 实例,但实际上,每个视图都在创建一个新的 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; }
    }
}

这是视图 A 代码隐藏:

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 实例?

已编辑

主视图和视图 A 中的代码已编辑,因此在主视图中我将 ViewModel 注册到容器中,并在 View AI 中解析视图模型。仍然得到三个实例。解析视图模型后,将创建一个新实例。

4

1 回答 1

0

正如理查兹建议的那样,我通过将视图模型注册为单例来解决这个问题:

container.RegisterInstance<ContactsViewModel>(contactsViewModel);
于 2017-01-02T14:48:42.617 回答