试图将代码变成切换

Trying to turn code into toggle

我正在尝试将以下代码变成一个开关,当用户按下向下按钮时,图像被设置为 var 路径,但是当用户再次按下向下按钮时,图像被设置为 var路径 2.

需要注意的是,当用户第一次进入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 它对任何人都有帮助。

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

您的计数器 (i) 在 OnDown() 方法的范围内。每次 OnDown() 被调用时, i 被重新初始化为 0,并且 0 % 2 始终等于 0,因此 if 块始终被触发。在 class 上创建一个 属性,我会使用一个布尔标志,你可以切换它,并在你的 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;
}