0

I am using the code like this. When lan is null, I am getting exception TypeInitializationException.

Is there any way to check null for URI??

ResourceDictionary dict = new ResourceDictionary();

dict.Source = new Uri("/Emdep.Geos.UI.Common;component/Resources/Language." + lan + ".xaml", UriKind.RelativeOrAbsolute);

    An unhandled exception of type 'System.TypeInitializationException' occurred in Emdep.Geos.UI.Common.dll## Heading ##

1 Answers1

1

What you should do is to catch the FileNotFoundException that occurs when the ResourceDictionary could not be found:

ResourceDictionary dict = new ResourceDictionary();
try
{
    dict.Source = new Uri("/Emdep.Geos.UI.Common;component/Resources/Language." + lan + ".xaml", UriKind.RelativeOrAbsolute);
}
catch (FileNotFoundException)
{
    //the resource dictionary could not be located/loaded...
}
mm8
  • 163,881
  • 10
  • 57
  • 88
  • it's an IOException that's thrown rather than a FileNotFoundException – Slate Feb 14 '19 at 15:57
  • @kjhf: No, it's not. – mm8 Feb 14 '19 at 16:00
  • ok dug some more, the exception I had was "Cannot locate resource '(uri)'" which I took to be file not found and was typed as IOException. However the real cause of the exception was because the assembly was not found rather than the file. Using your code as-is produces an IOException because my assembly doesn't know about `Emdep.Geos.UI.Common` – Slate Feb 14 '19 at 16:21