ConfigurationationManager.OpenMappedExeConfiguration 未在 XAML 设计器中加载配置文件
我创建了一个与 WPF 用户控件库关联的配置文件。在调试器中或单独运行应用程序时,使用以下代码可以很好地加载配置,该代码在库的上下文中运行:
ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
fileMap.ExeConfigFilename = "MapControl.dll.config";
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
if (config != null)
{
_Instance = (MapControlConfiguration)config.GetSection("MapControlConfiguration");
}
但是,当我尝试在 Visual Studio 2010 的 XAML 设计器中查看控件时,配置文件不会加载。使用进程监视器,我确定它正在尝试加载以下位置的配置文件:C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\MapControl.dll.config
。不幸的是,因为那肯定不是包含配置文件的目录。 ConfigurationManager.OpenMappedExeConfiguration
期望配置文件与可执行文件相关,但在 XAML 设计器的上下文中,本身没有可执行文件。有没有办法指定配置文件的位置,以便在 XAML 设计器中查看控件时加载该文件?
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 directory 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用 System.ComponentModel.DesignerProperties.GetIsInDesignMode 方法来判断您是否处于设计模式。就像这样:
You can use the
System.ComponentModel.DesignerProperties.GetIsInDesignMode
method to tell if you're in design mode or not. Like so: