Reputation: 4460
I am creating an app with Xamarin and I want to create a Loading Modal while I send and wait a response of a webservice. I was searching some examples and I found Modal Page and Rg.Plugins.PopUp but I still cannot do this works.
The problem with PopUp is that it always open after the response of webservice and not before and I can´t understant why it is happens.
How could I do this works ?
PopUp Page
<?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>
Searching on the 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();
}
Upvotes: 0
Views: 3252
Reputation: 69
private async void OnClickAcessar(object sender, EventArgs args){
//open popup loading with await
PopUpLoading loading = new PopUpLoading();
await 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 with await
await PopupNavigation.Instance.PopAsync();
// or used
// await PopupNavigation.Instance.PushAsync(loading, false);
}
Upvotes: 1
Reputation: 1666
The best loader for me in xamarin is this : 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 ();
}
})
);
}
Upvotes: 2
Reputation: 9990
While it can't be seen from your code, you are likely not executing the service with Task.Run
. That way it runs on the UI thread and prevents UI from updating. There are many answers on this topic, I myself gave several of them which contain more detailed explanation.
Upvotes: 0