我正在开发Windows Phone 7中的游戏。我想在主页上添加一个允许在图像之间切换的按钮。
我写了下面的代码,但没有工作
int key = 1;
switch (key)
{
case 1:
var brush = new ImageBrush();
BitmapImage image = new BitmapImage(new Uri(@"Assets/small/misc/music pause button.png", UriKind.Relative));
brush.ImageSource = image;
music.Background = brush;
key=0;
break;
case 0:
var brush2 = new ImageBrush();
BitmapImage image2 = new BitmapImage(new Uri(@"Assets/small/misc/music pause button.png", UriKind.Relative));
brush2.ImageSource = image2;
music.Background = brush2;
key = 1;
break;
}
答案 0 :(得分:1)
使用togglebutton
解决了这个问题在xaml中有切换按钮控件
<ToggleButton Name="tog" Margin="555,358,0,7" IsChecked="{x:Null}" Checked="tog_Checked" Unchecked="tog_Unchecked" Background="{x:Null}" BorderBrush="{x:Null}" BorderThickness="0" IsThreeState="False" HorizontalAlignment="Left" Width="123"></ToggleButton>
现在在事件处理程序上添加代码:
private void tog_Checked(object sender, RoutedEventArgs e)
{
tog.Background = brush;
togkey = 1;
System.Diagnostics.Debug.WriteLine("1");
}
private void tog_Unchecked(object sender, RoutedEventArgs e)
{
tog.Background = null;
togkey = 0;
System.Diagnostics.Debug.WriteLine("0");
}