0

我正在 Windows Phone 7 中开发游戏。我想在主页上添加一个按钮,允许在图像之间切换。

我写了以下代码但不起作用

整数键 = 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;

        }
4

1 回答 1

1

通过使用切换按钮解决了这个问题

在 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");
    }
于 2013-12-21T09:48:38.917 回答