ConfigurationationManager.OpenMappedExeConfiguration not loading configuration file in XAML designer
I've created a configuration file that's associated with a WPF User Control Library. When running the application in the debugger or on its own the config loads just fine using the following code, which is run in the context of the library:
ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
fileMap.ExeConfigFilename = "MapControl.dll.config";
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
if (config != null)
{
_Instance = (MapControlConfiguration)config.GetSection("MapControlConfiguration");
}
However, when I try to view the control in Visual Studio 2010's XAML designer, the configuration file won't load. Using Process Monitor, I've determined that it's trying to load the config file at the following location: C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\MapControl.dll.config
. This is unfortunate since that's certainly not the dir开发者_如何学Pythonectory containing the config file. ConfigurationManager.OpenMappedExeConfiguration
expects the config file to be relative to the executable, but in the context of the XAML designer there is no executable, per se. Is there any way to specify the location of the config file such that it will be loaded when the control is viewed in the XAML designer?
You can use the System.ComponentModel.DesignerProperties.GetIsInDesignMode
method to tell if you're in design mode or not. Like so:
ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
if (System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
{
fileMap.ExeConfigFilename = "F:\ull\Path\To\Your\Debug\MapControl.dll.config";
}
else
{
fileMap.ExeConfigFilename = "MapControl.dll.config";
}
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
if (config != null)
{
_Instance = (MapControlConfiguration)config.GetSection("MapControlConfiguration");
}
精彩评论