我正在使用Xamarin.Forms创建一个聊天机器人应用程序,当我发送消息并且聊天机器人回复聊天气泡时,请执行此递增循环堆栈操作。第一个答复有一个响应,第二个答复有两个,第三个答复有三个,依此类推。
我一直在使用一种称为Oscova Bot的模糊机器人框架,在他们的论坛上检查答案后,我认为它与事件处理程序有关,并且发生了不止一次或多次。这是C#代码。
/// <summary>
/// View model for the ChatbotPage.xaml
/// </summary>
public class ChatbotPageViewModel : BaseViewModel
{
/// <summary>
/// A field for TextToSend
/// </summary>
private string _texttosend;
/// <summary>
/// An Instance of a new SIML Oscova Chatbot
/// </summary>
public OscovaBot chatbot;
/// <summary>
/// A collection/list of chat message items
/// </summary>
public ObservableCollection<ChatMessageModel> Messages { get; set; } = new ObservableCollection<ChatMessageModel>();
/// <summary>
/// The text that the user inputs
/// </summary>
public string TextToSend
{
get
{
return _texttosend;
}
set
{
if (_texttosend != value)
{
_texttosend = value;
OnPropertyChanged();
}
}
}
/// <summary>
/// A command for sending the users messages
/// </summary>
public ICommand SendCommand { get; set; }
/// <summary>
/// ChatPageViewModel Constructor
/// </summary>
public ChatbotPageViewModel()
{
SendCommand = new RelayCommand(Send);
chatbot = new OscovaBot();
var assembly = IntrospectionExtensions.GetTypeInfo(typeof(MainPage)).Assembly;
Stream stream = assembly.GetManifestResourceStream("BluePillApp.Helpers.nice.siml");
chatbot.Import(XDocument.Load(stream));
chatbot.Trainer.StartTraining();
}
/// <summary>
/// This function sends a message
/// </summary>
public void Send()
{
if (!string.IsNullOrEmpty(TextToSend))
{
//This adds a new message to the messages collection
Messages.Add(new ChatMessageModel() { Text = TextToSend, User = App.User });
//This gets the chatbots response for each message
chatbot.MainUser.ResponseReceived += async (sender, args) =>
{
await Task.Delay(1500);
Messages.Add(new ChatMessageModel() { Text = args.Response.Text });
};
var result = chatbot.Evaluate(TextToSend);
result.Invoke();
//Removes the text in the Entry after message is sent
TextToSend = string.Empty;
}
}
}
@@ Lucas Zhang-MSFT您好,这是Draxalot2(发布此问题的人),我似乎忘记了我帐户的凭据,所以我现在已经创建了这个新帐户。这是项目的GitHub链接:https://github.com/Bhyte-Softwares/BluePillApp-Repo