diff --git a/src/NLogViewer/NLogViewer.xaml.cs b/src/NLogViewer/NLogViewer.xaml.cs index 90a85fe..53b3354 100644 --- a/src/NLogViewer/NLogViewer.xaml.cs +++ b/src/NLogViewer/NLogViewer.xaml.cs @@ -476,6 +476,7 @@ public NLogViewer() private void _OnUnloaded(object sender, RoutedEventArgs e) { // look in logical and visual tree if the control has been removed + // If there is no parent window found before, we have a special case (https://github.com/dojo90/NLogViewer/issues/30) and just dispose it anyway if (_ParentWindow.FindChildByUid(Uid) == null) { _Dispose(); @@ -498,8 +499,14 @@ private void _OnLoaded(object sender, RoutedEventArgs e) Loaded -= _OnLoaded; // add hook to parent window to dispose subscription - _ParentWindow = Window.GetWindow(this); - _ParentWindow.Closed += _ParentWindowOnClosed; + // use case: + // NLogViewer is used in a new window inside of a TabControl. If you switch the TabItems, + // the unloaded event is called and would dispose the subscription, even if the control is still alive. + if (Window.GetWindow(this) is { } window) + { + _ParentWindow = window; + _ParentWindow.Closed += _ParentWindowOnClosed; + } ListView.ScrollToEnd(); var target = CacheTarget.GetInstance(targetName: TargetName);