I'm developing an Excel add-in using multiple windows forms and multiple Element host objects to contain WPF controls. I also have a form which calls to a web service. And displays the results back to Excel.
The problem I'm currently facing is the current : After some interaction with the addin ( no specific order has been found ) my addin stops behaving correctly, throwing an invalid operation exception somewhere inside the PresentationFramework.dll . I can't inspect the code that's running in there because it's hidden.
The Exception says the following : "Application object is being shut down".
What could be causing this?
It is thrown at initialization of a control that is used for some custom drawings
InitializeComponent();
The stack trace looks like this :
at System.Windows.Application.GetResourcePackage(Uri packageUri)\r\n at System.Windows.Application.GetResourceOrContentPart(Uri uri)\r\n at System.Windows.Application.LoadComponent(Object component, Uri resourceLocator)\r\n at Drawing.MoleculeView.InitializeComponent() in d:\Projects\Common\Depict\Drawing\MoleculeView.xaml:line 1\r\n at Drawing.MoleculeView..ctor() in d:\Projects\Common\Depict\Drawing\MoleculeView.xaml.cs:line 192\r\n at DrawingControlWrapper.MultipleDrawingControl.LoadMolecule(Molecule molecules, Point[] bounds) in c:\Users\Nikolay\Desktop\Addin\DrawingControlWrapper\MultipleDrawingControl.xaml.cs:line 32\r\n at CallSite.Target(Closure , CallSite , MultipleDrawingControl , Object , Point[] )\r\n at LMCExcelFunctions.ThisAddIn.QueueMolecule(Object mol) in c:\Users\Nikolay\Desktop\Addin\LMCExcelFunctions\ThisAddIn.cs:line 182\r\n at CallSite.Target(Closure , CallSite , Type , Object )\r\n at LMCExcelFunctions.ThisAddIn.addin_SheetSelectionChange(Object sh, Range target) in c:\Users\Nikolay\Desktop\Addin\LMCExcelFunctions\ThisAddIn.cs:line 93
XAML looks like this :
<UserControl x:Class="Drawing.MoleculeView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<DockPanel x:Name="layout" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
</DockPanel>
</UserControl>
If you set Application.Current.ShutdownMode = ShutdownMode.OnExplicitShutdown you problem should go away.
Wpf controls need an instance of application but but winForm doesn't instantiate the wpf application. you can resolve it by the following code at application startup:
System.Windows.Application app = new System.Windows.Application()
{
ShutdownMode = Windows.ShutdownMode.OnExplicitShutdown
};
Closed += () =>
{
app.Shutdown();
};
or in Vb.net:
Dim app As System.Windows.Application = New System.Windows.Application With {
.ShutdownMode = Windows.ShutdownMode.OnExplicitShutdown
AddHandler Closed, Sub()
app.Shutdown()
End Sub
Resolved by using an inner Element Host : it appears that any control that is being created by a control which is hosted inside an element host must also have its own element host.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With