【问题标题】:How to add button name to dictionary in c#?如何在 C# 中将按钮名称添加到字典中?
【发布时间】:2020-10-05 05:20:56
【问题描述】:

我正在编写一个我想要的程序 Dictionary<String, List<int>> nameColor= new Dictionary<String, List<int>>();

我有 32 个按钮,每个按钮都有一个名称“Btn00”到“Btn32”。我正在更改单击按钮的颜色,并且我想要包含按钮名称和颜色列表的字典。然后我将解析为 json 以得到{"Btn00":[0,0,0],"Btn01":[255,0,255], etc.}。我有滑块,点击时可以改变颜色:

List<int> btnColors = new List<int>();
public void changeColor(Button button)
    {
        byte rr = (byte)SeekR.Value;
        byte gg = (byte)SeekG.Value;
        byte bb = (byte)SeekB.Value;
        Color cc = Color.FromRgb(rr, gg, bb); //Create object of Color class.
        SolidColorBrush colorBrush = new SolidColorBrush(cc); //Creating object of SolidColorBruch class.
        button.Background = colorBrush; //Setting background of a button.

        btnColors.Add(rr);
        btnColors.Add(gg);
        btnColors.Add(bb);

我在这里调用 changeColor 函数:

private void changeBtnColor(object sender, RoutedEventArgs e)
    {
        changeColor(sender as Button);
    }

我试过了

String btnName;
Dictionary<String, List<int>> nameColor= new Dictionary<String, List<int>>();

但我不知道如何将按钮名称作为字符串放入字典中,然后将其解析为 JSON,这样我就有了上述符号。

编辑:

我这样更改了我的代码:

public void changeColor(Button button)
    {


        List<int> btnColors = new List<int>();
        Dictionary<String, List<int>> nameColor= new Dictionary<String, List<int>>();
        byte rr = (byte)SeekR.Value;
        byte gg = (byte)SeekG.Value;
        byte bb = (byte)SeekB.Value;
        Color cc = Color.FromRgb(rr, gg, bb); //Create object of Color class.
        SolidColorBrush colorBrush = new SolidColorBrush(cc); //Creating object of SolidColorBruch class.
        button.Background = colorBrush; //Setting background of a button.
                                        //ledIndBack.Background = colorBrush;

            btnColors.Add(rr);
            btnColors.Add(gg);
            btnColors.Add(bb);

            nameColor.Add(button.Name, btnColors);


        string json = JsonConvert.SerializeObject(nameColor);
        Console.WriteLine(json);
    }

我得到像 {"Btn00":[255,0,0]} {"Btn10":[0,0,255]} 等 json 值。所以它不在同一个对象中,我可能需要使用loop 和 SerializeObject 在循环外,我尝试过,但每次得到相同的响应。此外,这些代码在我的 xaml.cs 文件中。这会是一个问题,还是我应该在我的 ViewModel 文件夹 -> BtnViewModel.cs 中添加命令和使用函数。我还得把我的数据发送到服务器,不知道在xaml.cs中做所有事情有多聪明。

【问题讨论】:

标签: c# json wpf list dictionary


【解决方案1】:

制作元组字典而不是字符串字典,然后解析字符串
Dictionary&lt;int, (int r, int g, int b)&gt;
https://docs.microsoft.com/en-us/dotnet/csharp/tuples

由于您只有 32 个值,因此您也可以进行切换。文档上有一个例子。 https://docs.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-8

public static RGBColor FromRainbow(Rainbow colorBand) =>  
    colorBand switch  
    {  
        Rainbow.Red    => new RGBColor(0xFF, 0x00, 0x00),  
        Rainbow.Orange => new RGBColor(0xFF, 0x7F, 0x00),  
        Rainbow.Yellow => new RGBColor(0xFF, 0xFF, 0x00),  
        Rainbow.Green  => new RGBColor(0x00, 0xFF, 0x00),  
        Rainbow.Blue   => new RGBColor(0x00, 0x00, 0xFF),  
        Rainbow.Indigo => new RGBColor(0x4B, 0x00, 0x82),  
        Rainbow.Violet => new RGBColor(0x94, 0x00, 0xD3),  
        _              => throw new ArgumentException(message: "invalid enum value", paramName: nameof(colorBand)),
    };  

但是,我仍然会使用元组;它比开关更灵活。

【讨论】:

    猜你喜欢
    • 2014-05-21
    • 2022-01-03
    • 2023-03-03
    • 1970-01-01
    • 1970-01-01
    • 2021-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多