I have a bizarre problem that is causing us problems.
We have a simple C# Wpf UserControl. Its a progress bar – nothing fancy – just a border that changes size depending on bindings to Value and MaxValue. It works fine 99% of the time and we use it in a dozen or so places in out code – including the splash screen.
It always works fine in the splash screen – so it always loads and works in our app.
The problem is that sometimes (and we can’t predict or understand when) the InitializeComponent() call in the progress bar constructor throws a System.Exception. Looking into the progressbar.g.cs file created during compile (obj/Debug folder) I can see that the exception is thrown because the progressbar.xaml file is not found. I have not, of course, changed any code in the g.cs file and I haven’t done anything fancy in this UserControl at all.
System.Exception: The component 'MyProject.ControlLibrary.ProgressBar' does not have a resource identified by the URI '/MyProject.ControlLibrary;component/progressbar.xaml'.
at System.Windows.Application.LoadComponent(Object component, Uri resourceLocator)
at MyProject.ControlLibrary.ProgressBar.InitializeComponent() in d:\Projectfolder\MyProject.ControlLibrary\ProgressBar.xaml:line 1
at MyProject.ControlLibrary.ProgressBar..ctor() in D:\projectfolder\MyProject.ControlLibrary\ProgressBar.xaml.cs:line 26
at ProjectName.UI.VideoViewer..ctor() in D:\projectfolder\UI\VideoViewer.xaml.cs:line 26
Source: PresentationFramework
I don’t understand why the progressbar.xaml resource would go missing sometimes, especially given that it always works fine at least once when the app is run.
The UserControl is contained in a project called MyProject.ControlLibrary.dll. This project seems to be configured correctly as it contains other usercontrols which don’t show any problems – except from one other similar UserControl which has identical problems.
I have run out of ideas on this – so any suggestions would be helpful. I am using VisualStudio 2008 and .net 3.5
I may have solved this – I’ll explain the problem here in case someone else runs into it.
One section of our code dynamically loads plugins from a plugins folder. To do this, it does Assembly.LoadFile(filename) for all the .dll files in the plugins folder.
It checks to see if the dll implements our plugins interface; if it does, it loads it, if not, it doesn’t. Our plugin interfaces are contained in a file called MyProject.Interfaces.dll and this file, along with MyProject.ControlLibrary.dll are also in the plugin’s folder.
So, the code sometimes calls Assembly.LoadFile on MyProject.ControlLibrary.dll twice as it cycles through the dlls in the plugins folder. It seems that following this second LoadFile, the xaml resources become lost. When I prevent this second load, it seems to fix the problem.