【问题标题】:How to place a button(inline keyboard) in a column on the telegram-api?如何在电报API的列中放置一个按钮(内联键盘)?
【发布时间】:2020-09-24 17:51:02
【问题描述】:

我尝试为电报机器人创建自定义键盘。 使用 AmatuerDev 的解决方案:create dynamic Keyboard telegram bot in c# , MrRoundRobin API 它看起来像这样:

button in row

如何在列中放置按钮?

谢谢!

附:源代码:

private static InlineKeyboardButton[][] GetInlineKeyboard(string [] 
stringArray)
{
var keyboardInline = new InlineKeyboardButton[1][];
var keyboardButtons = new InlineKeyboardButton[stringArray.Length];
for (var i = 0; i < stringArray.Length; i++)
{
    keyboardButtons[i] = new InlineKeyboardButton
    {
        Text = stringArray[i],
        CallbackData = "Some Callback Data",
    };
}
keyboardInline[0] = keyboardButtons;
return keyboardInline;
}

【问题讨论】:

  • 您的意思是如何将此键盘拆分为 2 行而不是 1 行?
  • 是的。我需要更多 10 行。

标签: c# telegram telegram-bot


【解决方案1】:

注意事项:

  1. GetInlineKeyboard 的返回类型更改为InlineKeyboardMarkup 而不是InlineKeyboardButton[][]
  2. 使用显式类型而不是隐式类型 (var)
  3. 使用 List&lt;InlineKeyboardButton&gt; 代替数组 (InlineKeyboardButton[])
  4. 不要创建具有相同回调数据的按钮
  5. 替换方法参数取IDictionary&lt;string, string&gt;获取文本和回调数据

每个IEnumerable&lt;InlineKeyboardButton&gt; 都是单行,这意味着你应该传递行列表。

你不应该使用var keyboardInline = new InlineKeyboardButton[1][];,因为这意味着这一行中只有一行有按钮!

有以下两种方式:


你想要的代码:

我不建议使用此代码

private static InlineKeyboardButton[][] GetInlineKeyboard(string[] stringArray)
{
    var keyboardInline = new InlineKeyboardButton[stringArray.Length][];

    for (var i = 0; i < stringArray.Length; i++)
    {
        keyboardInline[i] = new InlineKeyboardButton[]
        {
            new InlineKeyboardButton
            {
                Text = stringArray[i],
                CallbackData = "Some Callback Data",
            };
        }
    }

    return keyboardInline;
}

最佳方式:

我建议这个代码:

private static InlineKeyboardMarkup GetInlineKeyboard(IDictionary<string, string> buttonsData)
{
    // Rows Count
    int count = buttonsData.Length;

    // List of rows 
    // Every 'List<InlineKeyboardButton>' is row
    List<List<InlineKeyboardButton>> buttons = new List<List<InlineKeyboardButton>>(count);    

    for (int i = 0; i < count; i++)
    {
        // Add new row with one button capacity
        buttons.Add(new List<InlineKeyboardButton>(1)
        {
            // Add button to this row
            new InlineKeyboardButton
            {
                Text = buttonsData.Values.ElementAt(i),
                CallbackData = buttonsData.Keys.ElementAt(i),
            }
        });
    }
    
    return new InlineKeyboardMarkup(buttons);
}

【讨论】:

  • 最佳方式不起作用:(错误 CS1503 参数 1:无法从 'Telegram.Bot.Types.ReplyMarkups.InlineKeyboardButton[]' 转换为 'System.Collections.Generic.List
  • 我按照“最佳方式”中提供的登录信息,设法将 inlineButtons 显示在另一个之上。我投票的原因。感谢 Muaath 的解决方案
【解决方案2】:

您需要将每个按钮放入数组中。 Reply markup: keyboard & inline keyboard

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-11-02
    • 2017-06-04
    • 2018-02-09
    • 2021-09-02
    • 2018-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多