0

我在此msdn 文章中使用应用程序设置为声音创建了布尔变量。现在我想在我的菜单中toggleswitch设置音量开或关。我正在考虑使用类似this answer的东西。但我不确定这是否对我的问题有好处。我应该将图像变量添加到我的 AppSettings 还是有更好的方法来做到这一点?

我的解决方案:

在xml中:

<ToggleButton Name="tgbSound" BorderThickness="0" Background="Transparent"
                      Checked="tgbSound_Checked"
                      Unchecked="tgbSound_Unchecked"
                      IsChecked="{Binding Source={StaticResource appSettings}, Path=SoundSetting, Mode=TwoWay}">
        </ToggleButton>

在 xaml 页面的代码中:

private void tgbSound_Checked(object sender, RoutedEventArgs e)
{
    SetTgbSoundContentTo("Images/volumeon.png");
}

private void tgbSound_Unchecked(object sender, RoutedEventArgs e)
{
    SetTgbSoundContentTo("Images/volumeoff.png");
}

private void SetTgbSoundContentTo(string uri)
{
    Image volumeoff = new Image();
    ImageSource zdroj = new BitmapImage(new Uri(uri, UriKind.Relative));
    volumeoff.Source = zdroj;
    volumeoff.Height = 40;
    if (tgbSound == null)
        return;
    tgbSound.Content = volumeoff;
    tgbSound.Background = new SolidColorBrush(Colors.Transparent);
}
4

1 回答 1

4

你可以两者都做。对于这两种方法,我将建议您使用一种通用方法。

Create a Boolean property in your AppSettings and create its wrapper inside your viewmodel. Then bind (two way) the wrapper property in your view model to your UI element, that can be both a toggle switch or an image. In case of toggleswitch, the two way binding will itself do the trick, but for an image you will have to handle the tap event and handle the on/off states and Boolean values in the code yourself.

于 2013-01-07T05:15:45.713 回答