【发布时间】:2020-06-15 07:27:49
【问题描述】:
我正在构建一个聊天机器人应用程序,其中包含用于传入和传出消息的聊天气泡。对于传入的消息,我给了它一个Task.Delay(),现在我想在每次消息弹出之前给它一个ActivityIndicator(即我想在消息被延迟时显示活动指示器) .我已将活动指示器添加到传入消息控件的 XAML;
IncomingMessageItemControl
<ViewCell
x:Class="BluePillApp.Controls.IncomingMessageItemControl"
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:pancake="clr-namespace:Xamarin.Forms.PancakeView;assembly=Xamarin.Forms.PancakeView"
mc:Ignorable="d">
<Grid x:Name="Gridoo">
<pancake:PancakeView
Margin="10,10,80,10"
Padding="15"
BackgroundColor="#53ffc6"
CornerRadius="20,20,0,20"
HasShadow="False"
HorizontalOptions="StartAndExpand">
<Label
FontSize="Medium"
Text="{Binding Text}"
TextColor="#1a1a1a" />
</pancake:PancakeView>
<ActivityIndicator IsRunning="True" IsVisible="True" />
</Grid>
</ViewCell>
问题是,在 ChatbotMessagingPage 中,按下发送按钮,然后在收到回复/传入消息之前发送传出消息,我已经在 MVVM 中这样做了;
ChatbotMessagingPageViewModel
//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 });
};
}
#region CLASS METHODS
/// <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);
var result = chatbot.Evaluate(TextToSend);
result.Invoke();
//Removes the text in the Entry after message is sent
TextToSend = string.Empty;
}
}
每次我按下发送按钮时,ActivityIndicator 都会与 IncomingMessage 一起出现,我希望 ActivityIndicator 先出现,而 IncomingMessage 被延迟。
【问题讨论】:
-
既然你已经得到消息,那么延迟添加消息是没有意义的,而是延迟元素的呈现。你可以做褪色动画 - Xamarin Forms Animation documentation
-
我不想延迟添加消息。每条消息在显示之前都有一个延迟,我想要的是在延迟时弹出一个活动指示器
-
那么你应该有一个不同的策略......将忙碌光标放在列表的页脚,等待消息时显示......简单,让我知道你是否需要更多帮助
标签: asynchronous xamarin.forms task