试图将代码转换为切换

时间:2017-12-04 01:50:23

标签: c# wpf xaml toggle

我试图将以下代码转换为切换,当用户按下向下按钮时,图像被设置为var路径,但是当用户再次按下向下按钮时,图像被设置为var path2。

重要的是要注意,当用户第一次进入UI时,根本没有显示任何图像(因为下面的代码中的Controller2尚未初始化)

这就是我之前所拥有的:

public bool OnDown(bool held)
    {
            string path = $"pack://siteoforigin:,,,/Themes/Test/Images/Controls/Atari 2600/Joystick.png";

            BitmapImage bitmap = new BitmapImage();

            bitmap.BeginInit();

            bitmap.UriSource = new Uri(path, UriKind.Absolute);

            bitmap.EndInit();

            Controller2.Source = bitmap;
            return true;
    }

上面的代码将Controller 2图像设置为Atari 2600.png,从而显示图像。

这是我尝试将代码转换为切换:

public bool OnDown(bool held)
    {
        var i = 0;

        if (i % 2 == 0)
        {
            string path = $"pack://siteoforigin:,,,/Themes/Test/Images/Controls/Atari 2600/Joystick.png";

            BitmapImage bitmap = new BitmapImage();

            bitmap.BeginInit();

            bitmap.UriSource = new Uri(path, UriKind.Absolute);

            bitmap.EndInit();

            Controller2.Source = bitmap;

            i++;
        } else
        {
            string path2 = $"pack://siteoforigin:,,,/Themes/Test/Images/Controls/Atari 5200/Joystick.png";

            BitmapImage bitmap2 = new BitmapImage();

            bitmap2.BeginInit();

            bitmap2.UriSource = new Uri(path2, UriKind.Absolute);

            bitmap2.EndInit();

            Controller2.Source = bitmap2;

            i++;
        }

        return true;
    }

不幸的是,当用户再次按下向下按钮时,图像不会更改为Atari 5200.png。它只停留在Atari 2600.png

OnDown方法源自following API,它可以帮助任何人。

帮助弄清楚如何让切换工作将不胜感激。谢谢。

1 个答案:

答案 0 :(得分:2)

您的计数器(i)属于OnDown()方法的范围。每次调用OnDown()时,i都会重新初始化,值为0,0 % 2始终等于0,因此始终会触发if块。在类上创建一个属性,我会使用一个布尔标志来切换,并在if/else逻辑中使用它。这是.NET中可变范围的msdn article

private bool Use2600 { get; set; }    

public bool OnDown(bool held)
    {
        if (Use2600)
        {
            string path = $"pack://siteoforigin:,,,/Themes/Test/Images/Controls/Atari 2600/Joystick.png";

            BitmapImage bitmap = new BitmapImage();

            bitmap.BeginInit();

            bitmap.UriSource = new Uri(path, UriKind.Absolute);

            bitmap.EndInit();

            Controller2.Source = bitmap;
        } else
        {
            string path2 = $"pack://siteoforigin:,,,/Themes/Test/Images/Controls/Atari 5200/Joystick.png";

            BitmapImage bitmap2 = new BitmapImage();

            bitmap2.BeginInit();

            bitmap2.UriSource = new Uri(path2, UriKind.Absolute);

            bitmap2.EndInit();

            Controller2.Source = bitmap2;
        }

        Use2600 = !Use2600;

        return true;
    }

由于你是C#的新手,而且它似乎是一般的编码,我还会提供一些清理代码的建议......

private bool Use2600 { get; set; }    

// This method can return void, because always returning true is pointless
public void OnDown(bool held)
{
    // The only real logic in this method is which string to use, so the
    // if/else block and duplication of code can be reduced to the following
    // by using a ternary operator, and string interpolation
    string path = $"pack://siteoforigin:,,,/Themes/Test/Images/Controls/Atari {(Use2600 ? "2600" : "5200")}/Joystick.png"

    Controller2.Source = new BitmapImage(new Uri(path));

    Use2600 = !Use2600;
}