弹出式窗口是否正在Xamarin中加载?

时间:2019-04-18 17:44:12

标签: 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>

在Web服务上搜索

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

3 个答案:

答案 0 :(得分:1)

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

        }
    })
    );
}

答案 1 :(得分:0)

虽然从代码中看不到它,但是您可能没有使用Task.Run执行该服务。这样,它可以在UI线程上运行,并防止UI更新。关于该主题的答案很多,我本人也给出了其中的几个答案,其中包含更详细的解释。

答案 2 :(得分:0)

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);
        }