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

Using a TabControl triggers a TargetInvocationException at App shutdown, when the WPF Control is hosted in a Winform Elementhost #3439

Open
svengl opened this issue Jan 20, 2024 · 2 comments
Labels
bug evaluation required Items is pending review or evaluation by the team

Comments

@svengl
Copy link

svengl commented Jan 20, 2024

Bug explanation

To reproduce this bug, create a Winform app (tested in .NET 8), add an Elementhost (add from code behind since it is not in the Toolbox anymore...) and host a WPF UserControl which contains a TabControl. This will display fine until you close the app. During shutdown, the TargetInvocationException will be thrown. The call stack is hardly helpful since these are shutdown calls. Other controls are apparently not affected and work fine.

Setting the TabControl Style to x:Null will not trigger the exception anymore, so it is definitely something in the TabControl style.

The "source" object of the exception being found in the WeakEventManager.cs seems to be a Scrollviewer whose parent is a "ColorZone". Hope this hint helps.

Version

4.9.0

@svengl svengl added bug evaluation required Items is pending review or evaluation by the team labels Jan 20, 2024
@svengl
Copy link
Author

svengl commented Feb 10, 2024

I investigated this a little bit
The culprit are the following attached properties in the TabControl style:
Inside the ControlTemplate, one can find:

<ScrollViewer wpf:ScrollViewerAssist.BubbleVerticalScroll="True" wpf:ScrollViewerAssist.SupportHorizontalScroll="True"

Removing these attached properties fixes the issue.
I digged a bit further and see that the ScrollViewerAssist uses a WeakEventManager to listen to the ScrollViewer Loaded/Unloaded Events:

In ScrollViewerAssist.cs:
WeakEventManager<ScrollViewer, RoutedEventArgs>.AddHandler(scrollViewer, nameof(ScrollViewer.Loaded), OnLoaded); WeakEventManager<ScrollViewer, RoutedEventArgs>.AddHandler(scrollViewer, nameof(ScrollViewer.Unloaded), OnUnloaded);

When the Winform App with the Elementhost shuts down, the stacktrace indeed shows that the error happens during the cleanup of the WeakEventManager

System.Private.CoreLib.dll!System.Reflection.MethodBaseInvoker.InvokeDirectByRefWithFewArgs(object obj, System.Span copyOfArgs, System.Reflection.BindingFlags invokeAttr) Line 176 C#
System.Private.CoreLib.dll!System.Reflection.MethodBaseInvoker.InvokeWithOneArg(object obj,
System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, object[] parameters, System.Globalization.CultureInfo culture) Line 122 C#
WindowsBase.dll!System.Windows.WeakEventManager.Purge(object source, object data, bool purgeAll) Line 671 C#
WindowsBase.dll!MS.Internal.WeakEventTable.Purge(bool purgeAll) Line 295 C#
WindowsBase.dll!MS.Internal.WeakEventTable.OnShutDown() Line 359 C#

The actual exception is: System.Reflection.TargetInvocationException: 'Exception has been thrown by the target of an invocation. The calling thread cannot access....

I am a little clueless how to fix this, since the exception occurs in .net code?! Anybody any idea?

@Foreverrrrrr
Copy link

I also encountered this problem, and this exception did not appear in 4.8 of the MaterialDesignThemes version

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug evaluation required Items is pending review or evaluation by the team
Projects
None yet
Development

No branches or pull requests

2 participants