自适应卡片 - DateInput 和 SubmitAction
Adaptive Card - DateInput and SubmitAction
主要问题
我在 Microsoft Bot Framework 中使用自适应卡片。我 运行 遇到了一个问题,我不知道如何在用户选择他想要的日期后从 DateInput 中获取值。以下是我目前的代码:
public async Task StartAsync(IDialogContext context)
{
string pIdentifier = serviceFactory.ProfileService.GetPatientIdentifier(nric);
string response = serviceFactory.DentalAppointmentService.GetSlotSearchDates(nric,apptId,caseNumber, institutionCode, pIdentifier);
string[] split = response.Split(' ');
DateTime earliestStartDate = DateTime.ParseExact(split[0], "yyyy'-'MM'-'dd'T'HH':'mm':'ss", CultureInfo.InvariantCulture);
DateTime latestEndDate = DateTime.ParseExact(split[1], "yyyy'-'MM'-'dd'T'HH':'mm':'ss", CultureInfo.InvariantCulture);
await context.PostAsync("Your appointment can only be rescheduled within this period " + earliestStartDate.ToShortDateString() + " to " + latestEndDate.ToShortDateString());
AdaptiveCard card = new AdaptiveCard();
card.Body.Add(new TextBlock()
{
Text = "Enter new Date",
Size = TextSize.Large,
Weight = TextWeight.Bolder
});
DateInput input = new DateInput()
{
Id = "Date",
Placeholder = "New Date"
};
card.Body.Add(input);
card.Actions.Add(new SubmitAction()
{
Title = "Submit"
});
Attachment cardAttachment = new Attachment()
{
ContentType = AdaptiveCard.ContentType,
Content = card
};
var message = context.MakeMessage();
message.Attachments = new List<Attachment>();
message.Attachments.Add(cardAttachment);
await context.PostAsync(message);
context.Wait(this.MessageReceivedAsync);
}
private async Task MessageReceivedAsync(IDialogContext context, IAwaitable<object> result)
{
var temp = await result as Activity;
string date = temp.Text;
//DateTime newDate = DateTime.ParseExact(value.ToString(), "yyyy'-'MM'-'dd'T'HH':'mm':'ss", CultureInfo.InvariantCulture);
Debug.WriteLine("Entered Msg received Async:" + date);
await context.PostAsync(date);
}
当前抛出的错误
我目前 运行 遇到这个问题,但找不到解决方案:
Exception thrown: 'Microsoft.CSharp.RuntimeBinder.RuntimeBinderException' in mscorlib.dll
来自 DatePicker 的日期值将作为 activity.Value
上的 JObject 进入机器人
以下代码将从 .Text 属性 或 .Value 中提取日期:
public async Task StartAsync(IDialogContext context)
{
context.Wait(this.MessageReceivedAsync);
}
public virtual async Task MessageReceivedAsync(IDialogContext context, IAwaitable<IMessageActivity> result)
{
var temp = await result as Activity;
DateTime dateTime;
var datePresent = DateTime.TryParse(temp.Text, out dateTime);
if (!datePresent && temp.Value != null)
{
var jObjectValue = temp.Value as JObject;
var dateAsString = jObjectValue.Value<string>("Date");
if (!string.IsNullOrEmpty(dateAsString))
{
dateTime = DateTime.ParseExact(dateAsString, "yyyy-MM-dd", CultureInfo.InvariantCulture);
datePresent = true;
}
}
if (!datePresent)
{
//since the user did not send a date, show the card
AdaptiveCard card = new AdaptiveCard();
card.Body.Add(new AdaptiveTextBlock()
{
Text = "Enter new Date",
Size = AdaptiveTextSize.Large,
Weight = AdaptiveTextWeight.Bolder
});
AdaptiveDateInput input = new AdaptiveDateInput()
{
Id = "Date",
Placeholder = "New Date"
};
card.Body.Add(input);
card.Actions.Add(new AdaptiveSubmitAction()
{
Title = "Submit"
});
Attachment cardAttachment = new Attachment()
{
ContentType = AdaptiveCard.ContentType,
Content = card
};
var message = context.MakeMessage();
message.Attachments = new List<Attachment>();
message.Attachments.Add(cardAttachment);
await context.PostAsync(message);
}
else
{
await context.PostAsync($"Date Entered: {dateTime}");
}
context.Wait(this.MessageReceivedAsync);
}
此外,请将您的 AdaptiveCards 库升级到 1.0 版:https://www.nuget.org/packages/AdaptiveCards/
主要问题
我在 Microsoft Bot Framework 中使用自适应卡片。我 运行 遇到了一个问题,我不知道如何在用户选择他想要的日期后从 DateInput 中获取值。以下是我目前的代码:
public async Task StartAsync(IDialogContext context)
{
string pIdentifier = serviceFactory.ProfileService.GetPatientIdentifier(nric);
string response = serviceFactory.DentalAppointmentService.GetSlotSearchDates(nric,apptId,caseNumber, institutionCode, pIdentifier);
string[] split = response.Split(' ');
DateTime earliestStartDate = DateTime.ParseExact(split[0], "yyyy'-'MM'-'dd'T'HH':'mm':'ss", CultureInfo.InvariantCulture);
DateTime latestEndDate = DateTime.ParseExact(split[1], "yyyy'-'MM'-'dd'T'HH':'mm':'ss", CultureInfo.InvariantCulture);
await context.PostAsync("Your appointment can only be rescheduled within this period " + earliestStartDate.ToShortDateString() + " to " + latestEndDate.ToShortDateString());
AdaptiveCard card = new AdaptiveCard();
card.Body.Add(new TextBlock()
{
Text = "Enter new Date",
Size = TextSize.Large,
Weight = TextWeight.Bolder
});
DateInput input = new DateInput()
{
Id = "Date",
Placeholder = "New Date"
};
card.Body.Add(input);
card.Actions.Add(new SubmitAction()
{
Title = "Submit"
});
Attachment cardAttachment = new Attachment()
{
ContentType = AdaptiveCard.ContentType,
Content = card
};
var message = context.MakeMessage();
message.Attachments = new List<Attachment>();
message.Attachments.Add(cardAttachment);
await context.PostAsync(message);
context.Wait(this.MessageReceivedAsync);
}
private async Task MessageReceivedAsync(IDialogContext context, IAwaitable<object> result)
{
var temp = await result as Activity;
string date = temp.Text;
//DateTime newDate = DateTime.ParseExact(value.ToString(), "yyyy'-'MM'-'dd'T'HH':'mm':'ss", CultureInfo.InvariantCulture);
Debug.WriteLine("Entered Msg received Async:" + date);
await context.PostAsync(date);
}
当前抛出的错误
我目前 运行 遇到这个问题,但找不到解决方案:
Exception thrown: 'Microsoft.CSharp.RuntimeBinder.RuntimeBinderException' in mscorlib.dll
来自 DatePicker 的日期值将作为 activity.Value
上的 JObject 进入机器人以下代码将从 .Text 属性 或 .Value 中提取日期:
public async Task StartAsync(IDialogContext context)
{
context.Wait(this.MessageReceivedAsync);
}
public virtual async Task MessageReceivedAsync(IDialogContext context, IAwaitable<IMessageActivity> result)
{
var temp = await result as Activity;
DateTime dateTime;
var datePresent = DateTime.TryParse(temp.Text, out dateTime);
if (!datePresent && temp.Value != null)
{
var jObjectValue = temp.Value as JObject;
var dateAsString = jObjectValue.Value<string>("Date");
if (!string.IsNullOrEmpty(dateAsString))
{
dateTime = DateTime.ParseExact(dateAsString, "yyyy-MM-dd", CultureInfo.InvariantCulture);
datePresent = true;
}
}
if (!datePresent)
{
//since the user did not send a date, show the card
AdaptiveCard card = new AdaptiveCard();
card.Body.Add(new AdaptiveTextBlock()
{
Text = "Enter new Date",
Size = AdaptiveTextSize.Large,
Weight = AdaptiveTextWeight.Bolder
});
AdaptiveDateInput input = new AdaptiveDateInput()
{
Id = "Date",
Placeholder = "New Date"
};
card.Body.Add(input);
card.Actions.Add(new AdaptiveSubmitAction()
{
Title = "Submit"
});
Attachment cardAttachment = new Attachment()
{
ContentType = AdaptiveCard.ContentType,
Content = card
};
var message = context.MakeMessage();
message.Attachments = new List<Attachment>();
message.Attachments.Add(cardAttachment);
await context.PostAsync(message);
}
else
{
await context.PostAsync($"Date Entered: {dateTime}");
}
context.Wait(this.MessageReceivedAsync);
}
此外,请将您的 AdaptiveCards 库升级到 1.0 版:https://www.nuget.org/packages/AdaptiveCards/