我编写了这段代码,以便用户可以在我的应用程序中静音或取消静音。
private void VolumeButton_Click(object sender, RoutedEventArgs e)
{
if(Muted==false)
{
Muted = true;
}
if (Muted == true)
{
Muted = false;
}
}
我写了这样的soundplayer
方法:
public void playSound(string path)
{
var p1 = new System.Windows.Media.MediaPlayer();
if (Muted == false)
{
p1.Open(new Uri(path, UriKind.Relative));
p1.Play();
}
}
如果我点击应用程序无关紧要的静音按钮,它仍然会播放声音,好像什么都没发生一样。
答案 0 :(得分:2)
如评论中所述(BradleyDotNET提供),问题在您的VolumeButton_Click
方法中。这两个if语句都在执行,这意味着Muted
总是是假的:
if (Muted == false)
{
Muted = true;
}
// Muted now equals true, regardless.
if (Muted == true)
{
Muted = false;
}
您想要的是if /else if
方法:
if (Muted == false)
{
Muted = true;
}
else if (Muted == true)
{
Muted = false;
}