2
所以我有我打电话,像这样这个对话框链...麻烦与对话链
public static readonly IDialog<string> dialog = Chain.PostToChain()
.Select(msg => msg.Text)
.Switch(
new RegexCase<IDialog<string>>(new Regex("^hi", RegexOptions.IgnoreCase), (context, txt) =>
{
return Chain.ContinueWith(new GreetingDialog(),
async (ctx, res) =>
{
var token = await res;
var name = "User";
context.UserData.TryGetValue<string>("Name", out name);
return Chain.Return($"You are logged in as: {name}");
});
}),
new DefaultCase<string, IDialog<string>>((context, txt) =>
{
int count;
context.UserData.TryGetValue("count", out count);
context.UserData.SetValue("count", ++count);
string reply = string.Format("{0}: You said {1}", count, txt);
return Chain.Return(reply);
}))
.Unwrap()
.PostToUser();
对话框看起来像这样
[Serializable]
public class GreetingDialog : IDialog
{
public async Task StartAsync(IDialogContext context)
{
await context.PostAsync("Hi I'm John Bot");
context.Wait(MessageReceivedAsync);
}
public async Task MessageReceivedAsync(IDialogContext context, IAwaitable<IMessageActivity> argument)
{
var message = await argument;
var userName = String.Empty;
var getName = false;
context.UserData.TryGetValue<string>("Name", out userName);
context.UserData.TryGetValue<bool>("GetName", out getName);
if (getName)
{
userName = message.Text;
context.UserData.SetValue<string>("Name", userName);
context.UserData.SetValue<bool>("GetName", false);
}
if (string.IsNullOrEmpty(userName))
{
await context.PostAsync("What is your name?");
context.UserData.SetValue<bool>("GetName", true);
}
else
{
await context.PostAsync(String.Format("Hi {0}. How can I help you today?", userName));
}
context.Wait(MessageReceivedAsync);
}
}
,当我尝试运行此我得到回来,说错误“:Microsoft.Bot.Builder.Internals.Fibers.ClosureCaptureException:异常,捕捉环境匿名方法闭包是不可序列,考虑取消环境捕获或使用反射序列化代理”
有人能指出我在正确的方向?我不确定我在这里做错了什么。
OMG太感谢你了!这就是诀窍! – Matt