[英]WP7 - switching sound on/off using icon
我在此msdn文章中使用应用程序设置为声音创建了布尔变量。 现在,我希望在菜单toggleswitch
其作为toggleswitch
,可以将音量设置为开或关。 我正在考虑使用这种答案 。 但是我不确定这对我的问题是否有帮助。 我应该在我的AppSettings中添加图像变量还是有更好的方法?
我的解决方案:
在xaml中:
<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);
}
两者都可以。 对于这两种方法,我都会建议您采用一种通用方法。
在您的AppSettings中创建一个布尔属性,并在视图模型中创建其包装。 然后将视图模型中的wrapper属性绑定(双向)到UI元素,该UI元素既可以是拨动开关,也可以是图像。 在使用toggleswitch
情况下, two way binding
本身可以解决问题,但是对于图像,您将不得不自己处理代码中的tap事件并处理开/关状态和布尔值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.