Bot Framework (.Net) - Hero Card 按钮点击 - 具有自定义类型的 IAwaitable
Bot Framework (.Net) - Hero Card button click - IAwaitable with a custom type
我有一个 class(比方说 XYZ),我正在将类型 XYZ 的列表传递给Hero Card 作为按钮,这样当用户点击按钮时,它 returns 被点击的项目的价值。我有一个名为 getResult 的方法,它需要一个 XYZ.
类型的值
private async Task getResult(IDialogContext context, IAwaitable<XYZ> result)
{}
以上方法执行自context.Wait<XYZ>(getResult);
但是当我点击一个按钮时,出现错误(InvalidTypeException: invalid type: expected Microsoft.MscaBot.Data.XYZ, have Activity
)。
如何避免此错误并接收用户点击的项目?
更新:(以下是我的代码的样子)
[Serializable]
public class XYZ
{
public override string ToString()
{
return name;
}
public string name { get; set; }
public Nullable<decimal> price { get; set; }
}
其他class:
[Serializable]
public class TestDialog : IDialog<object>
{
private static List<XYZ> listOfXYZ = new List<XYZ>() { new XYZ() { name = "MyName1", price = 20.23 }, new XYZ() { name = "MyName2", price = 50.63 } };
public async Task StartAsync(IDialogContext context)
{
await ShowOptions (context);
}
private async Task ShowOptions(IDialogContext context)
{
var replyToConversation = context.MakeMessage();
replyToConversation.Type = "message";
List<CardAction> cardButtons = new List<CardAction>();
foreach (var item in listOfXYZ)
{
CardAction CardButton = new CardAction()
{
Type = ActionTypes.ImBack,
Title = item.ToString(),
Value = item
};
cardButtons.Add(CardButton);
}
HeroCard heroCard = new HeroCard()
{
Text = "Please select one of the options below",
Buttons = cardButtons,
};
Attachment myAttachment = heroCard.ToAttachment();
replyToConversation.Attachments.Add(myAttachment);
replyToConversation.AttachmentLayout = "list";
await context.PostAsync(replyToConversation);
context.Wait(getResult);
}
private async Task getResult(IDialogContext context, IAwaitable<IMessageActivity> result)
{
// Some logic here
}
}
HeroCard
(或任何其他卡片)的 CardAction 最终会向机器人发送 IMessageActivity
。 activity 的文本将是您在 CardAction
.
中设置的值
您尝试执行的操作是不可能的。您需要等待 IMessageActivity
。如果您尝试发送一个复杂对象作为值,请确保该对象是可序列化的,否则它也不会工作。
我有一个 class(比方说 XYZ),我正在将类型 XYZ 的列表传递给Hero Card 作为按钮,这样当用户点击按钮时,它 returns 被点击的项目的价值。我有一个名为 getResult 的方法,它需要一个 XYZ.
类型的值private async Task getResult(IDialogContext context, IAwaitable<XYZ> result)
{}
以上方法执行自context.Wait<XYZ>(getResult);
但是当我点击一个按钮时,出现错误(InvalidTypeException: invalid type: expected Microsoft.MscaBot.Data.XYZ, have Activity
)。
如何避免此错误并接收用户点击的项目?
更新:(以下是我的代码的样子)
[Serializable]
public class XYZ
{
public override string ToString()
{
return name;
}
public string name { get; set; }
public Nullable<decimal> price { get; set; }
}
其他class:
[Serializable]
public class TestDialog : IDialog<object>
{
private static List<XYZ> listOfXYZ = new List<XYZ>() { new XYZ() { name = "MyName1", price = 20.23 }, new XYZ() { name = "MyName2", price = 50.63 } };
public async Task StartAsync(IDialogContext context)
{
await ShowOptions (context);
}
private async Task ShowOptions(IDialogContext context)
{
var replyToConversation = context.MakeMessage();
replyToConversation.Type = "message";
List<CardAction> cardButtons = new List<CardAction>();
foreach (var item in listOfXYZ)
{
CardAction CardButton = new CardAction()
{
Type = ActionTypes.ImBack,
Title = item.ToString(),
Value = item
};
cardButtons.Add(CardButton);
}
HeroCard heroCard = new HeroCard()
{
Text = "Please select one of the options below",
Buttons = cardButtons,
};
Attachment myAttachment = heroCard.ToAttachment();
replyToConversation.Attachments.Add(myAttachment);
replyToConversation.AttachmentLayout = "list";
await context.PostAsync(replyToConversation);
context.Wait(getResult);
}
private async Task getResult(IDialogContext context, IAwaitable<IMessageActivity> result)
{
// Some logic here
}
}
HeroCard
(或任何其他卡片)的 CardAction 最终会向机器人发送 IMessageActivity
。 activity 的文本将是您在 CardAction
.
您尝试执行的操作是不可能的。您需要等待 IMessageActivity
。如果您尝试发送一个复杂对象作为值,请确保该对象是可序列化的,否则它也不会工作。