Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ParentWindow is null #30

Closed
olluz opened this issue Oct 7, 2020 · 7 comments
Closed

ParentWindow is null #30

olluz opened this issue Oct 7, 2020 · 7 comments

Comments

@olluz
Copy link

olluz commented Oct 7, 2020

can you please check if the Parentwindow is null ?

If I add the UserControl as an ElementHost child (e.g. in my Outlook Addin) then it'll throw me an exception, because it is not loaded in a Window...

here:
if (_ParentWindow.FindChildByUid(Uid) == null)
and here:
_ParentWindow.Closed += _ParentWindowOnClosed;

Thanks

@djonasdev
Copy link
Owner

djonasdev commented Oct 7, 2020

Thanks for the hint! This hook was inserted so that the following issue could be solved: #16

The UserControl is currently disposed of via the _ParentWindow.Closed EventHandler. That would no longer be the case and we would have a memory leak.

If you have a solution for me to solve your problem, I am happy to implement it!

@olluz
Copy link
Author

olluz commented Oct 7, 2020

I think you had this check before, but removed it for some reason.

May I suggest this simple change:
_ParentWindow = Window.GetWindow(this);
if (_ParentWindow != null)
{
_ParentWindow.Closed += _ParentWindowOnClosed;
}
ListView.ScrollToEnd();

@djonasdev
Copy link
Owner

@olluz
Copy link
Author

olluz commented Oct 7, 2020

The Unloaded event will still be called. And even though the _ParentWindow below is null it will not throw an exception (which is surprising to me).

private void _OnUnloaded(object sender, RoutedEventArgs e) { // look in logical and visual tree if the control has been removed if (_ParentWindow.FindChildByUid<NLogViewer>(Uid) == null) { _Dispose(); } }

So, Dispose is called in _OnUnloaded.

I don't see any down side in checking if the _ParentWindow is null, because if it is the WindowClosed event will never be called anyway...

@djonasdev
Copy link
Owner

djonasdev commented Oct 8, 2020

The Unloaded event will still be called. And even though the _ParentWindow below is null it will not throw an exception (which is surprising to me).

Since the method is called recursive, no exception should be thrown here. See: https://github.com/dojo90/NLogViewer/blob/master/src/NLogViewer/Extensions/DependencyObjectExtensions.cs#L20

I don't see any down side in checking if the _ParentWindow is null, because if it is the WindowClosed event will never be called anyway...

This logic is needed if you use the NLogViewer on a TabControl inside of a new window/popup. If you change the TabItem, the UnLoaded handler is called and we need to check if the popup window is closed or just another TabItem is selected.

Could you please try the following commit whether it solves your problem? 92fb12b

@djonasdev
Copy link
Owner

Ah now I think I get it! 🤦‍♂️😂
Since null is returned if _ParentWindow == null I don't need the if else query!

@olluz
Copy link
Author

olluz commented Oct 8, 2020

yes, that works. Thanks a lot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants