0

I'm trying to bind via xaml an app.config value to a control. Here's the code:

//App.config:
<appSettings>
    <add key="SoundFile" value="C:\\Windows\\Media\\Windows Hardware Fail.wav"/>
</appSettings>

//window.xaml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:properties="clr-namespace:MfClient.Properties">
...
<SoundPlayerAction Source="{Binding Source={x:Static p:Settings.Default},Path=SoundFile}"/>

This gives this error:

Error 44 Unknown build error, 'Key cannot be null. Parameter name: key Line 30 Position 56.'

If I remove the binding everything turns out to be ok:

<SoundPlayerAction Source="C:\Windows\Media\Windows Hardware Fail.wav" />

In order to bind (w/o the two way approach that I don't need), I've followed this and this other threads.

What am I getting wrong?

4

1 回答 1

0

您应该将 SoundplayerAction 绑定到您创建的 ViewModel 或 AppconfigProvider 类。在该类上创建一个读取 appconfig 值的只读属性。

您可以将该类添加为视图上的统计资源。

这样,您还可以随时轻松更改获取该路径的位置,并将多个应用程序设置组合到一个提供程序类中

于 2013-06-24T08:50:32.580 回答