xamarin - 在 Xamarin 中弹出加载?
问题描述
我正在使用 Xamarin 创建一个应用程序,并且我想在发送和等待 Web 服务的响应时创建一个加载模式。我正在搜索一些示例,发现 Modal Page 和 Rg.Plugins.PopUp 但我仍然无法完成这项工作。
PopUp 的问题在于它总是在 web 服务响应之后而不是之前打开,我无法理解它为什么会发生。
我怎么能做到这一点?
弹出页面
<?xml version="1.0" encoding="utf-8" ?>
<pages:PopupPage
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:pages="clr-namespace:Rg.Plugins.Popup.Pages;assembly=Rg.Plugins.Popup"
xmlns:animations="clr-namespace:Rg.Plugins.Popup.Animations;assembly=Rg.Plugins.Popup"
x:Class="CooperativaApp.View.PopUpLoading"
CloseWhenBackgroundIsClicked="False">
<!--You can set an animation in the xaml file or in the csharp code behind-->
<pages:PopupPage.Animation>
<animations:ScaleAnimation
PositionIn="Center"
PositionOut="Center"
ScaleIn="1.2"
ScaleOut="0.8"
DurationIn="400"
DurationOut="300"
EasingIn="SinOut"
EasingOut="SinIn"
HasBackgroundAnimation="True"
/>
</pages:PopupPage.Animation>
<!--You can use any elements here which are extended from Xamarin.Forms.View-->
<StackLayout
VerticalOptions="Center"
HorizontalOptions="Center"
Padding="20, 20, 20, 20"
BackgroundColor="White">
<ActivityIndicator
IsRunning="True"
IsVisible="True"
Color="Green"/>
</StackLayout>
</pages:PopupPage>
在 WebService 上搜索
private void OnClickAcessar(object sender, EventArgs args){
//open popup loading
PopUpLoading loading = new PopUpLoading();
PopupNavigation.Instance.PushAsync(loading, true);
//user object
Usuario usuario = new Usuario();
usuario.login = "admin";
usuario.pswd = "admin";
//webservice login
UsuarioService service = new UsuarioService();
service.doLogin(usuario);
//close popup loading
PopupNavigation.Instance.PopAsync();
}
解决方案
在 xamarin 中对我来说最好的加载器是这样的:https ://github.com/aritchie/userdialogs
void CallService ()
{
Device.BeginInvokeOnMainThread (() => UserDialogs.Instance.ShowLoading ("Loading ...", MaskType.Black));
Task.Run (() => {
//webservice login
UsuarioService service = new UsuarioService();
service.doLogin(usuario);
}).ContinueWith (result => Device.BeginInvokeOnMainThread (() => {
UserDialogs.Instance.HideLoading ();
}
})
);
}
推荐阅读
- bash - 从预接收钩子提交 - 错误:未能推送一些参考
- python - 在列表中链接自定义函数的 Pythonic 方法
- gitlab - Omnibus Gitlab SSH 连接问题
- sql - 在 SSRS 中使用我希望成为日期/时间变量的参数设置数据驱动订阅
- node.js - Socket.io 和 Express 与 nginx
- apache-spark - 为什么 BigDecimal 的 Spark groupBy.agg(min/max) 总是返回 0?
- sql - 左外连接到表的前 1 行
- java - GSON 自动将 JSON 数组解包为 Java 对象
- package - 如何区分哪些软件包是“官方”或标准 Racket 发行版的一部分?
- java - Eclipse直接调试到接口方法