ConfigurationationManager.OpenMappedExeConfiguration 未在 XAML 设计器中加载配置文件

发布于 2024-10-11 12:44:52 字数 799 浏览 7 评论 0原文

我创建了一个与 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏至、离别 2024-10-18 12:44:52

您可以使用 System.ComponentModel.DesignerProperties.GetIsInDesignMode 方法来判断您是否处于设计模式。就像这样:

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");
}

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");
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文