public static void RemoveHandler(LongLivedViewModel source, EventHandler handler) { _ = source ?? throw new ArgumentNullException(nameof(source)); _ = handler ?? throw new ArgumentNullException(nameof(handler)); CurrentManager.ProtectedRemoveHandler(source, handler); }
private void ToggleEventHandlingMechanism(object sender, RoutedEventArgs e) { _longLivedViewModel = new LongLivedViewModel(); _testWeakEventManager = (EventPattern)((int)(_testWeakEventManager + 1) % 3); DataContext = _longLivedViewModel; TestWeakEventManager(_longLivedViewModel); }
public void ShortLivedViewModel_CustomWeakEventManagerReferenceToLongLivedViewModel_HandlesEventRaisedOnLongLivedViewModel() { var longLivedViewModel = new LongLivedViewModel(); var shortLivedViewModel = new ShortLivedViewModel(longLivedViewModel, EventPattern.CustomEventManager); // act longLivedViewModel.RaiseEvent(); Assert.AreEqual("ShortLivedViewModel - handled event", longLivedViewModel.Log); }
ShortLivedViewModel_StrongReferenceToLongLivedViewModel_ShortLivedViewModelUnreferenced_AttemptGarbageCollection_ShortLivedViewModelKeptAliveByStrongReference_HandlesEventRaisedOnLongLivedViewModel() { var longLivedViewModel = new LongLivedViewModel(); var shortLivedViewModel = new ShortLivedViewModel(longLivedViewModel, EventPattern.StrongReference); shortLivedViewModel = null; GC.Collect(); // act longLivedViewModel.RaiseEvent(); Assert.AreEqual("ShortLivedViewModel - handled event", longLivedViewModel.Log); }
ShortLivedViewModel_CustomWeakEventManagerReferenceToLongLivedViewModel_ShortLivedViewModelUnreferenced_AttemptGarbageCollection_ShortLivedViewModelGarbageCollected() { var longLivedViewModel = new LongLivedViewModel(); var shortLivedViewModel = new ShortLivedViewModel(longLivedViewModel, EventPattern.CustomEventManager); var weakReferenceShortLivedViewModel = new WeakReference(shortLivedViewModel); shortLivedViewModel = null; // act GC.Collect(); Assert.IsFalse(weakReferenceShortLivedViewModel.IsAlive); }
ShortLivedViewModel_StrongReferenceToLongLivedViewModel_ShortLivedViewModelUnreferenced_AttemptGarbageCollection_ShortLivedViewModelKeptAliveByStrongReference() { var longLivedViewModel = new LongLivedViewModel(); var shortLivedViewModel = new ShortLivedViewModel(longLivedViewModel, EventPattern.StrongReference); var weakReferenceShortLivedViewModel = new WeakReference(shortLivedViewModel); shortLivedViewModel = null; // act GC.Collect(); Assert.IsTrue(weakReferenceShortLivedViewModel.IsAlive); }
ShortLivedViewModel_CustomWeakEventManagerReferenceToLongLivedViewModel_ShortLivedViewModelUnreferenced_AttemptGarbageCollection_ShortLivedViewModelGarbageCollected_DoesNotHandleEventRaisedOnLongLivedViewModel() { var longLivedViewModel = new LongLivedViewModel(); var shortLivedViewModel = new ShortLivedViewModel(longLivedViewModel, EventPattern.CustomEventManager); shortLivedViewModel = null; GC.Collect(); // act longLivedViewModel.RaiseEvent(); Assert.IsEmpty(longLivedViewModel.Log); }
private void TestWeakEventManager(LongLivedViewModel longLivedViewModel) { switch (_testWeakEventManager) { case EventPattern.StrongReference: longLivedViewModel.Log = "Using strong reference"; break; case EventPattern.GenericWeakEventManager: longLivedViewModel.Log = "Using generic WeakEventManager"; break; case EventPattern.CustomEventManager: longLivedViewModel.Log = "Using custom WeakEventManager"; break; } var shortLivedViewModel = new ShortLivedViewModel(longLivedViewModel, _testWeakEventManager); var weakReferenceShortLivedViewModel = new WeakReference(shortLivedViewModel); longLivedViewModel.Log = "Raise event on LongLivedViewModel"; // expect shortLivedViewModel to handle event longLivedViewModel.RaiseEvent(); longLivedViewModel.Log = "Clear reference to ShortLivedViewModel"; shortLivedViewModel = null; longLivedViewModel.Log = "Force garbage collection"; GC.Collect(); longLivedViewModel.Log = $"ShortLivedViewModel.IsAlive = {weakReferenceShortLivedViewModel.IsAlive}"; longLivedViewModel.Log = "Raise event on LongLivedViewModel"; // shortLivedViewModel will only handle the event if not using the WeakEventManager longLivedViewModel.RaiseEvent(); }