我有一个内容页面,通过我的Xamarin应用程序中的SMTP服务器发送电子邮件。
点击提交按钮发送电子邮件后,应用程序只会等待。在此等待期间,我想显示一个活动指示符或一个文本加载标签,因此在显示该进程成功的DisplayAlert之前,用户知道某些内容正在运行。
由于某种原因,未显示ActivityIndicator和Label Text。 也许我做错了什么。
XAML
<StackLayout x:Name="myPop" AbsoluteLayout.LayoutBounds="0, 0, 1, 1"
AbsoluteLayout.LayoutFlags="All" BackgroundColor="#C0808080" Padding="5">
<ContentView x:Name="input_box_overlay"
AbsoluteLayout.LayoutBounds="0, 0, 1, 1"
AbsoluteLayout.LayoutFlags="All"
Padding="5">
<StackLayout Padding="20" BackgroundColor="White"
HorizontalOptions="Center" VerticalOptions="Center"
HeightRequest="230" WidthRequest="230">
<Label Text="Enter suggestion" TextColor="Black" FontSize="Medium"/>
<StackLayout Padding="0, 10, 0, 0">
<Editor x:Name="user_text" HeightRequest="100" Keyboard="Chat" BackgroundColor="#f7f8f9"/>
</StackLayout>
<StackLayout HorizontalOptions="Center" VerticalOptions="Center">
<Label Text="Submitting..." x:Name="load"/>
<ActivityIndicator x:Name="indicator"/>
</StackLayout>
<StackLayout Orientation="Horizontal" VerticalOptions="EndAndExpand" HorizontalOptions="CenterAndExpand">
<Button TextColor="White" Text="Cancel" Clicked="Cancel_Clicked" BackgroundColor="#C0808080"/>
<Button TextColor="White" Text="Submit" Clicked="Submit_Clicked" BackgroundColor="#395368" />
</StackLayout>
</StackLayout>
</ContentView>
</StackLayout>
Submit_Clicked
方法/事件的代码
private async void Submit_Clicked(object sender, EventArgs e)
{
try
{
indicator.IsRunning = true;
indicator.IsVisible = true;
indicator.IsEnabled = true;
load.IsVisible = true;
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtp.sendgrid.net");
mail.From = new MailAddress("email@domain.com");
mail.To.Add("email@domain.com");
mail.Subject = "Subject";
mail.Body = user_msg;
SmtpServer.Port = 25;
SmtpServer.Credentials = new NetworkCredential("username", "password");
SmtpServer.EnableSsl = true;
ServicePointManager.ServerCertificateValidationCallback = delegate (object sendemail, X509Certificate certificate, X509Chain chain, System.Net.Security.SslPolicyErrors sslPolicyErrors)
{
return true;
};
SmtpServer.SendCompleted += (s, ev) => {
indicator.IsRunning = false;
indicator.IsVisible = false;
indicator.IsEnabled = false;
load.IsVisible = false;
};
SmtpServer.Send(mail);
await DisplayAlert("Success", "Message Sent. Thank you.", "Ok");
myPop.IsVisible = false;
myPop.IsEnabled = false;
}
catch (Exception ex)
{
await DisplayAlert("Error", "Something went wrong. Please try again.", "ok");
Console.WriteLine(ex.ToString());
}
}
答案 0 :(得分:3)
考虑保持代码异步并且不阻止UI,这是SmtpClient.Send
这是为允许非阻塞流程而提供的原始代码的重构
private async void Submit_Clicked(object sender, EventArgs e) {
try {
ToggleIndicator(true);
await SendEmailAsync();
ToggleIndicator(false);
await DisplayAlert("Success", "Message Sent. Thank you.", "Ok");
myPop.IsVisible = false;
myPop.IsEnabled = false;
} catch (Exception ex) {
await DisplayAlert("Error", "Something went wrong. Please try again.", "ok");
Console.WriteLine(ex.ToString());
}
}
private void ToggleIndicator(bool show) {
indicator.IsRunning = show;
indicator.IsVisible = show;
indicator.IsEnabled = show;
load.IsVisible = show;
}
private async Task SendEmailAsync() {
MailMessage mail = new MailMessage();
mail.From = new MailAddress("email@domain.com");
mail.To.Add("email@domain.com");
mail.Subject = "Subject";
mail.Body = user_msg;
SmtpClient SmtpServer = new SmtpClient("smtp.sendgrid.net");
SmtpServer.Port = 25;
SmtpServer.Credentials = new NetworkCredential("username", "password");
SmtpServer.EnableSsl = true;
ServicePointManager.ServerCertificateValidationCallback = delegate (object sendemail, X509Certificate certificate, X509Chain chain, System.Net.Security.SslPolicyErrors sslPolicyErrors) {
return true;
};
await SmtpServer.SendMailAsync(mail);
}
虽然XAML看起来不错,但我还是假设标签和指标可见性最初是假的
<StackLayout HorizontalOptions="Center" VerticalOptions="Center">
<Label Text="Submitting..." x:Name="load" IsVisible="False"/>
<ActivityIndicator x:Name="indicator" IsVisible="False"/>
</StackLayout>