4

I have an unmanaged application which uses a WPF assembly for some of its user interface.

Initially I was getting exceptions like:

System.IO.FileNotFoundException was unhandled Message: Could not load file or assembly 'PresentationFramework ...

(Not for the same reason as Changed framework version results in: Could not load file or assembly PresentationFramework?).

PresentationFramework IS a reference of my assembly.

I have worked around this by adding a line of C# code that seems to force or trick the compiler / runtime into loading the DLL:

// Refers to an arbitrary enum in PresentationFramework.Classic.dll
var dummy = Microsoft.Windows.Themes.ClassicBorderStyle.None;

and then I get no exception at runtime.

Otherwise the only reference to anything in that DLL was in XAML. Specifically:

<ResourceDictionary Source="pack://application:,,,/PresentationFramework.Classic;component/themes/Classic.xaml"/>

But apparently this XAML reference is insufficient to get the DLL to be loaded correctly at runtime.


This 'dummy' solution is OK in that it does work, but I don't understand why this is necessary and it makes me think I've missed something more important.

Is this a correct workaround? What is the reason that this is necessary in the first place?

StayOnTarget
  • 11,743
  • 10
  • 52
  • 81

1 Answers1

0

XAML and C# are two disticnt languages, with seperate parser and compilers. Indeed the early debugger support for XAML Runtime Errors was somewhere around "non existent". And if you got a XAML Compiler error? The C# compiler would just use the last valid XAML compliation.

While you propably never use it, it is entirely possible to Dynamically load the XAML design at runtime: https://blogs.msmvps.com/bsonnino/2016/07/21/loading-xaml-dynamically-part-1-loading-views-dynamically/

At wich point the compiler can not rely on what is written in the XAML at - well compile time.

The Namespaces in XAML? They are only about getting .NET classes into the scope of the XAML parser. They do not realy mater for the C# part of the code that much.

Christopher
  • 9,634
  • 2
  • 17
  • 31