我有一个应用程序范围的标志,这是一个应用程序设置。
MyApp.Properties.Settings.Default.SoundMuted标志是如果我的应用程序声音是静音的。我有很多窗口,可以通过.Net的声音播放器播放声音。我在想,旗帜可以连接到一个带有命令的工具栏按钮上。我正在考虑静音通知,让我的播放声音线路的类挂起实现NotifyPropertyChange的应用程序的类(例如SoundManager)。然后,如果用户单击工具栏按钮,我将在SoundManager中设置Muted属性,并让所有soundplayer类获得PropertyChange和mute。
有没有更好的模式呢?假设我可以将所有的声音播放器连接到该命令,该命令将被触发。
另外,是否有一些巧妙的方法可以将应用程序设置连接为xaml中的bindable属性?
发布于 2012-11-04 20:27:11
虽然我总是做与我在另一个答案中相同的方法来敲打那个例子,但我已经意识到它是不必要的。您可以直接绑定到Settings对象,如下所示:
<Window x:Class="Test_WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MyApp"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<local:Settings x:Key="SettingsRes"/>
</Window.Resources>
<Grid>
<Menu>
<MenuItem Header="Audio">
<MenuItem Header="Mute" IsCheckable="True"
IsChecked="{Binding Path=Default.SoundMuted, Source={StaticResource ResourceKey=SettingsRes}}"/>
</MenuItem>
</Menu>
<TextBlock Text="{Binding Path=Default.SoundMuted, Source={StaticResource ResourceKey=SettingsRes}}" Height="23" HorizontalAlignment="Left" Margin="18,156,0,0" VerticalAlignment="Top" Width="75" />
</Grid>
</Window>同样,该资源的多个实例仍然可以很好地协同工作,例如将TextBlock绑定到第二个资源:
<Window x:Class="Test_WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MyApp"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<local:Settings x:Key="SettingsRes"/>
<local:Settings x:Key="SettingsRes2"/>
</Window.Resources>
<Grid>
<Menu>
<MenuItem Header="Audio">
<MenuItem Header="Mute" IsCheckable="True" IsChecked="{Binding Path=Default.SoundMuted, Source={StaticResource ResourceKey=SettingsRes}}"/>
</MenuItem>
</Menu>
<TextBlock Text="{Binding Path=Default.SoundMuted, Source={StaticResource ResourceKey=SettingsRes2}}" Height="23" HorizontalAlignment="Left" Margin="18,156,0,0" VerticalAlignment="Top" Width="75" />
</Grid>
</Window>发布于 2012-11-04 20:17:42
一种方法是创建一个包装类,以提供对静态Settings.Default的可绑定访问。
我后来意识到这比需要的工作量更多,请参阅我的另一个答案
namespace MyApp
{
internal sealed class ResourceWrapper
{
public Settings Default
{
get
{
return Settings.Default;
}
}
}
}现在,我们需要将其作为资源添加到某个地方,可以在App.xaml中完成,这里我在使用它的窗口中执行了本地操作,不要忘记命名空间:
xmlns:local="clr-namespace:MyApp"
<Window.Resources>
<local:ResourceWrapper x:Key="SettingsWrapper"/>
</Window.Resources>现在我们需要绑定到它,这显示了它在同一个窗口的MenuItem中使用:
<Menu>
<MenuItem Header="Audio">
<MenuItem Header="Mute" IsCheckable="True"
IsChecked="{Binding Path=Default.SoundMuted, Source={StaticResource ResourceKey=SettingsWrapper}}"/>
</MenuItem>
</Menu>就像我说的,你可以在应用程序级别添加资源,也可以在不同的窗口/控件中创建多个这样的ResourceWrappers,它们都指向相同的静态底层。
带有测试TextBlock window的完整xaml
<Window x:Class="Test_WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MyApp"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<local:ResourceWrapper x:Key="SettingsWrapper"/>
</Window.Resources>
<Grid>
<Menu>
<MenuItem Header="Audio">
<MenuItem Header="Mute" IsCheckable="True" IsChecked="{Binding Path=Default.SoundMuted, Source={StaticResource ResourceKey=SettingsWrapper}}"/>
</MenuItem>
</Menu>
<TextBlock Text="{Binding Path=Default.SoundMuted, Source={StaticResource ResourceKey=SettingsWrapper}}" Height="23" HorizontalAlignment="Left" Margin="18,156,0,0" VerticalAlignment="Top" Width="75" />
</Grid>
</Window>https://stackoverflow.com/questions/13214481
复制相似问题