I'm working on a WPF application in Visual Studio 2010.
When application windows are loaded into the designer, attempts to retrieve connection strings or app settings always return null. E.g., if I have an app setting called "foo" with value "bar", in the designer, ConnectionManager.AppSettings["foo"]
will return null, while at runtime, it will return "bar". This is causing me some consternation, since my window is now throwing exceptions in the designer.
Is this a known bug (or "feature") in Visual Studio? I haven't been able to find mention of it elsewhere.
If any of you have encountered this before, is there a common workaround available?
EDIT, per Phil's request:
This can be trivially replicated by placing a label in a Window, à la:
<Label Content="{Binding TheText}" HorizontalAlignment="Left" />
And in the viewmodel, setting the TheText element to the value of the application setting "foo":
TheText = ConfigurationManager.AppSettings["foo"];
The app.config contains the following appSettings section:
<appSettings>
<add key="foo" value="bar"/>
</appSettings>
EDIT: I've marked Phil's solution as the correct one, since it seems like using the settings API is the only way around this. Sadly, it's not a viable workaround in all cases. I've raised an issue on MS Connect about this: https://connect.microsoft.com/VisualStudio/feedback/details/738316/system-configuration-configurationmanager-does-not-work-in-wpf-designer