首页 > 解决方案 > 在 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

解决方案


在 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 ();

        }
    })
    );
}

推荐阅读