WPF命令模式

时间:2012-11-03 23:18:18

标签: wpf command

我有一个应用程序范围的标志,这是一个应用程序设置。

MyApp.Properties.Settings.Default.SoundMuted

标志是我的应用声音是否静音。我有很多窗户可以通过.Net的声音播放器播放声音。我以为标志会连接到带有命令的工具栏按钮。我正在考虑通过静音通知让我的课程播放声音连接到一个类(例如SoundManager)悬挂在实现NotifyPropertyChange的应用程序上。然后,如果用户单击工具栏按钮,我会在我的SoundManager中设置Muted属性,并让所有的soundplayer类获得PropertyChange和静音。

这有更好的模式吗?假设我可以将所有声音播放器连接到命令,并且该命令将会触发。

还有一些灵巧的方法可以将该应用设置连接为xaml中的可绑定属性吗?

2 个答案:

答案 0 :(得分:0)

一种方法是创建一个包装类来提供对静态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,它们都将指向相同的静态underneith。

带有测试TextBlock的窗口的完整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>

答案 1 :(得分:0)

虽然我总是采用与我的其他答案相同的方法来敲开那个例子,但我意识到它是不合情理的。您可以直接绑定到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>