2017-01-02 51 views
0

我有一個主視圖,帶有一個工具欄和一個有兩個視圖註冊的TabControl區域:視圖A,視圖B.所有視圖都應該具有與DataView相同的ContactsViewModel實例,但事實上,每個視圖都創建一個ContactsViewModel的新實例。查看模型不止一次地實例化。 (PRISM)

這是主視圖代碼隱藏:

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

代碼在主視圖和查看editted所以在主視圖中我註冊視圖模型到容器中,觀我解決的視圖模型。仍然得到三個實例。視圖模型解析後,會創建一個新實例。

+0

看起來你是從IoC容器獲得'ContactViewModel'。你有沒有想過把這個註冊爲單身人士? –

+0

我剛剛嘗試過,我仍然得到ContactsViewModel的三個實例。 (我用新的代碼編輯了我的問題)。我所做的是:在主視圖中,我將ContactsViewModel註冊到容器中;在視圖A中,我解析容器中的ContactsViewModel。這是你的意思嗎?偶爾發生的是,我每次解析ContactsViewModel時,都會創建一個新實例。 – chincheta73

+0

您並未將視圖模型註冊爲單例。在這裏學習如何做到這一點:https://msdn.microsoft.com/en-us/library/ff647854.aspx –

回答

0

理查茲的建議,我通過註冊視圖模型爲單固定的問題:

container.RegisterInstance<ContactsViewModel>(contactsViewModel);