public virtual void UnregisterService(IViewInjectionService service)
 {
     ServiceManager.Remove(service);
     if (Mode == ViewInjectionMode.Persistent)
     {
         foreach (object vm in service.ViewModels.ToList())
         {
             service.Remove(vm);
         }
     }
 }
Beispiel #2
0
        public void ViewModelKeyTest()
        {
            ViewInjectionService  service  = new ViewInjectionService();
            IViewInjectionService iService = service;
            ContentControl        target   = new ContentControl();

            Interaction.GetBehaviors(target).Add(service);
            object vm1 = new object();
            object vm2 = new object();

            Window.Content = target;
            EnqueueShowWindow();
            EnqueueCallback(() => {
                iService.Inject(null, null);
                iService.Inject(null, vm1);
                Assert.AreEqual(1, iService.ViewModels.Count());
                Assert.AreSame(vm1, iService.ViewModels.ElementAt(0));
                Assert.AreEqual(vm1, iService.GetKey(vm1));
                Assert.AreSame(vm1, iService.GetViewModel(vm1));

                AssertHelper.AssertThrows <InvalidOperationException>(() => iService.Inject(null, vm1),
                                                                      x => Assert.AreEqual("A view model with the same key already exists in the ViewInjectionService region.", x.Message));
                service.RegionName = "Test";
                AssertHelper.AssertThrows <InvalidOperationException>(() => iService.Inject(null, vm1),
                                                                      x => Assert.AreEqual("A view model with the same key already exists in the Test region.", x.Message));

                iService.Inject("New", vm2);
                Assert.AreEqual(2, iService.ViewModels.Count());
                Assert.AreSame(vm2, iService.ViewModels.ElementAt(1));
                Assert.AreEqual("New", iService.GetKey(vm2));
                Assert.AreSame(vm2, iService.GetViewModel("New"));

                iService.Remove(vm1);
                Assert.AreEqual(1, iService.ViewModels.Count());
                iService.Remove(vm2);
                Assert.AreEqual(0, iService.ViewModels.Count());
            });
            EnqueueTestComplete();
        }