视图模型实例化了多次.(棱镜)



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

已编辑

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

正如Richards所建议的,我通过将视图模型注册为singleton来解决这个问题:

container.RegisterInstance<ContactsViewModel>(contactsViewModel);

相关内容

  • 没有找到相关文章

最新更新