How to clear WPF / XAML cache in plugin project

I am writing a plugin for an application (Autodesk Revit Architecture 2011, if you need to know).

During debugging, I would like to recompile my plugin and reload it in the host. My particular host even provides an add-on manager that simplifies this process using Assembly.Load . For Windows.Forms plugins, this works like a charm.

When I use WPF, it breaks. At first, I got an error in these lines (I added some formatting to make it easier for you to read:

 System.Windows.Markup.XamlParseException: [A]MyApp.Controls.MyControl cannot be cast to [B]MyApp.Controls.MyControl. Type A originates from 'MyApp, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null' in the context 'LoadNeither' at location '%PATHA%'. Type B originates from 'MyApp, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null' in the context 'LoadNeither' at location '%PATHB'. Error at object 'MyApp.Controls.MyControl' in markup file 'MyApp;component/controls/mydialog.xaml'. 

I also took the liberty of renaming the controls, namespaces, and ways to protect the innocent and me, the culprit.

I assume this is because the XAML parser stores the cache memory of already loaded types.

My first step was to change the assembly versions by setting AssemblyInfo.cs/[assembly: AssemblyVersion("2.0.*") . It just pushes the error further:

 System.Windows.Markup.XamlParseException: Unable to cast object of type 'MyApp.Controls.MyControl' to type 'MyApp.Controls.MyControl'. Error at object 'MyApp.Controls.MyControl' in markup file 'MyApp;component/controls/mydialog.xaml'. ---> System.InvalidCastException: Unable to cast object of type 'MyApp.Controls.MyControl' to type 'MyApp.Controls.MyControl'. 
+4
source share
2 answers

It seems to me that your best bet is to compile in a separate AppDomain . You can then throw away this AppDomain after compilation.

The exact interface between your main AppDomain and the compilation of AppDomain is not really something I can comment on since you did not provide details in your question.

+3
source

I don’t know how to clear the cache, but as a workaround I would try to load XAML directly using XamlReader.Load and see if it works. Check out http://msdn.microsoft.com/en-us/library/ms590388.aspx#Y309

+2
source

Source: https://habr.com/ru/post/1338250/


All Articles