简体   繁体   English

WP7-使用图标打开/关闭声音

[英]WP7 - switching sound on/off using icon

I created boolean variable for sound with app settings in this msdn articles . 我在此msdn文章中使用应用程序设置为声音创建了布尔变量。 Now I want it in my menu as toggleswitch which can set volume on or off. 现在,我希望在菜单toggleswitch其作为toggleswitch ,可以将音量设置为开或关。 I am thinking about using something like this this answer . 我正在考虑使用这种答案 But I am not sure if it is good for my problem. 但是我不确定这对我的问题是否有帮助。 Should I add a variable for image to my AppSettings or is there a better way for doing this? 我应该在我的AppSettings中添加图像变量还是有更好的方法?

My solution: 我的解决方案:

In xaml: 在xaml中:

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

In code for xaml page: 在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);
}

You can do both. 两者都可以。 For both the methods, I will suggest you a common approach. 对于这两种方法,我都会建议您采用一种通用方法。

Create a Boolean property in your AppSettings and create its wrapper inside your viewmodel. 在您的AppSettings中创建一个布尔属性,并在视图模型中创建其包装。 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. 然后将视图模型中的wrapper属性绑定(双向)到UI元素,该UI元素既可以是拨动开关,也可以是图像。 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. 在使用toggleswitch情况下, two way binding本身可以解决问题,但是对于图像,您将不得不自己处理代码中的tap事件并处理开/关状态和布尔值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM