我正在使用Xamarin.Forms创建一个聊天机器人应用程序。每当我向机器人发送新消息时,都会收到回复,但会自动增加一个,即
User: Hi
Bot: Hello
User: How are you?
Bot: Good
Bot: Good
在代码中,我使用的是:
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, User = App.ChatBot });
};
var result = chatbot.Evaluate(TextToSend);
result.Invoke();
//Removes the text in the Entry after message is sent
TextToSend = string.Empty;
}
}
在Messages.Add(new ChatMessageModel() { Text = args.Response.Text, User = App.ChatBot });
上使用断点后,我发现它每次都会添加一条新消息,因此会不断增加。我希望有一种方法可以阻止这种情况,并且只发生一次。
编辑:整个类
using BluePillApp.Models;
using BluePillApp.ViewModels.Base;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Text;
using System.Windows.Input;
using Xamarin.Forms;
using Syn.Bot.Siml;
using Syn.Bot.Oscova;
using Syn.Bot.Oscova.Attributes;
using System.Reflection;
using System.IO;
using System.Threading.Tasks;
using System.Xml.Linq;
namespace BluePillApp.ViewModels
{
/// <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.new.siml");
chatbot.Import(XDocument.Load(stream));
chatbot.Trainer.StartTraining();
}
/// <summary>
/// This function sends a message
/// </summary>
public void Send()
{
if (!string.IsNullOrEmpty(TextToSend))
{
var msgModel = new ChatMessageModel() { Text = TextToSend, User = App.User };
//This adds a new message to the messages collection
Messages.Add(msgModel);
//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, User = App.ChatBot });
};
var result = chatbot.Evaluate(TextToSend);
result.Invoke();
//Removes the text in the Entry after message is sent
TextToSend = string.Empty;
}
}
}
}
参考方案
每次调用Send
时,您都将添加一个NEW事件处理程序
chatbot.MainUser.ResponseReceived +=
您只需要分配此事件处理程序一次
将谓词<T>转换为Func <T,bool> - c#我有一个包含成员Predicate的类,希望在Linq表达式中使用该类:using System.Linq; class MyClass { public bool DoAllHaveSomeProperty() { return m_instrumentList.All(m_filterExpression); } private IEnumerable&…
当我所有的都是T时,如何返回Interface <T>的实例? - java我有一个界面:public interface ILoginResult<T> { public T get(); } 我有一个LoginPage对象:public class LoginPage<T> { ... public ILoginResult<T> login(...) { ... } } 我也有一些登录页面对…
客户端反序列化为数组序列化字典<string,string>数据 - c#我有一个字典,该字典使用C#中的JavaScriptSerializer进行了序列化。在客户端,我有:"{"dd049eda-e289-4ca2-8841-4908f94d5b65":"2","ab969ac2-320e-42e1-b759-038eb7f57178":"5…
如何在Collection <T>上使用ToList - c#MSDN documentation表示类Collection<T>在扩展部分中具有方法ToList()。如何使用这种方法? 参考方案 该文档有点误导。类型Collection<T>没有直接使用此方法。相反,它被定义为System.Linq.Enumerable上的扩展方法。为using添加System.Linq指令应该可以解决此问题…
Maven的Shade插件产生的罐子之间有什么区别? - java在我的Maven项目中,我尝试使用maven-shade-plugin在运行mvn package时生成一个超级jar。结果,我的目标目录中出现三个jar:original-hello-world-0.1.0-SNAPSHOT.jar hello-world-0.1.0-SNAPSHOT.jar hello-world-0.1.0-SNAPSHOT-shad…