首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >WPF命令模式

WPF命令模式
EN

Stack Overflow用户
提问于 2012-11-04 07:18:18
回答 2查看 237关注 0票数 3

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

代码语言:javascript
复制
MyApp.Properties.Settings.Default.SoundMuted

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

有没有更好的模式呢?假设我可以将所有的声音播放器连接到该命令,该命令将被触发。

另外,是否有一些巧妙的方法可以将应用程序设置连接为xaml中的bindable属性?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-11-04 20:27:11

虽然我总是做与我在另一个答案中相同的方法来敲打那个例子,但我已经意识到它是不必要的。您可以直接绑定到Settings对象,如下所示:

代码语言:javascript
复制
<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绑定到第二个资源:

代码语言:javascript
复制
<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>
票数 0
EN

Stack Overflow用户

发布于 2012-11-04 20:17:42

一种方法是创建一个包装类,以提供对静态Settings.Default的可绑定访问。

我后来意识到这比需要的工作量更多,请参阅我的另一个答案

代码语言:javascript
复制
namespace MyApp
{
    internal sealed class ResourceWrapper
    {
        public Settings Default
        {
            get
            {
                return Settings.Default;
            }
        }
    }
}

现在,我们需要将其作为资源添加到某个地方,可以在App.xaml中完成,这里我在使用它的窗口中执行了本地操作,不要忘记命名空间:

代码语言:javascript
复制
xmlns:local="clr-namespace:MyApp"

<Window.Resources>
    <local:ResourceWrapper x:Key="SettingsWrapper"/>
</Window.Resources>

现在我们需要绑定到它,这显示了它在同一个窗口的MenuItem中使用:

代码语言:javascript
复制
<Menu>
    <MenuItem Header="Audio">
        <MenuItem Header="Mute" IsCheckable="True"
          IsChecked="{Binding Path=Default.SoundMuted, Source={StaticResource ResourceKey=SettingsWrapper}}"/>
    </MenuItem>
</Menu>

就像我说的,你可以在应用程序级别添加资源,也可以在不同的窗口/控件中创建多个这样的ResourceWrappers,它们都指向相同的静态底层。

带有测试TextBlock window的完整xaml

代码语言:javascript
复制
<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>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13214481

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档