简体   繁体   English

DisplayAlert 方法仅在 Xamarin 中的方法结束时执行

[英]DisplayAlert methods only executed at the end of the method in Xamarin

I'm writing a small Xamarin.Forms app and I try to do some actions like display alerts or show the Activity Indicator.我正在编写一个小型 Xamarin.Forms 应用程序,并尝试执行一些操作,例如显示警报或显示活动指示器。 However, everything what I wrote in my code is only executed at the end of the method, even the Activity Indicator, which is not the goal of course.但是,我在代码中编写的所有内容都只在方法结束时执行,即使是 Activity Indicator,这当然不是目标。 The different alerts are even showing in LIFO, so the last one in the code appears first.不同的警报甚至显示在 LIFO 中,因此代码中的最后一个首先出现。

Here's the code in xaml.cs:这是 xaml.cs 中的代码:

    private void btConnexion_Clicked(object sender, EventArgs e)
    {
        ai.IsRunning = true;
        IsBusy = true;
        LoginViewModel vm = (LoginViewModel)this.BindingContext;
        DisplayAlert(AppResources.Sorry, "Beginning", "Ok");

        try
        {
            DisplayAlert(AppResources.Sorry, "In the try", "Ok");

            if (vm.Valide())
            {
                Task t = Task.Run(async () => { await vm.AuthentificationAPI(); });
                if (!t.Wait(TimeSpan.FromSeconds(5)))
                {
                    DisplayAlert(AppResources.Sorry, AppResources.TryAgainLater, "Ok");
                }

                if (t.IsCompleted)
                {
                    GetAllData();
                    Application.Current.MainPage = new AppShell();
                    Shell.Current.GoToAsync("//main");

                }
            }
            else
                DisplayAlert(AppResources.Error, AppResources.MissingFieldsErrorMessage, "Ok");

        }
        catch (Exception ex)
        {
            DisplayAlert(AppResources.Sorry, AppResources.TryAgainLater + " (" + ex.Message + ")", "Ok");
        }
        finally
        {
            ai.IsRunning = false;
            DisplayAlert(AppResources.Sorry, "Finally", "Ok");


        }
        DisplayAlert(AppResources.Sorry, "After finally", "Ok");
    }

And where I call it in the .xaml:我在 .xaml 中调用它的地方:

        <Button Text="{x:Static resource:AppResources.Login}" 
                x:Name="btConnexion" 
                BackgroundColor="#AADC14" 
                Padding="0,0,0,0" 
                Clicked="btConnexion_Clicked"/>

I've put some alerts in the metohd, showing "In the try,", "Beginning", etc. If I debug the code step by step, all the alerts are only executed once at the end of the method, instead during the compilation.我在方法中放置了一些警报,显示“在尝试中”、“开始”等。如果我逐步调试代码,则所有警报仅在方法结束时执行一次,而不是在执行期间汇编。 And in FILO, so the first one is "After finally", then "Finally", etc. And the Activity Indicator is not even showing, because of the same reason I suppose.在 FILO 中,第一个是“最终之后”,然后是“最终”,等等。活动指示器甚至没有显示,因为我想的原因相同。

I don't know if it changes anything, but the xaml must be compiled as written in xaml.cs:我不知道它是否有任何改变,但必须按照 xaml.cs 中编写的方式编译 xaml:

[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class LoginPage : ContentPage

将 await 放在显示警报之前,如下所示

await DisplayAlert ("Alert", "You have been alerted", "OK");

I created a small instance to change the internal state of the DisplayAlert and ActivityIndicator methods.我创建了一个小实例来更改DisplayAlertActivityIndi​​cator方法的内部状态。

Here is cs code:这是cs代码:

public partial class MainPage : ContentPage
{
    public MainPage()
    {
        InitializeComponent();
    }

    //For DisplayAlert you only need to add await before the method.
    private async void btConnexion_Clicked(object sender, EventArgs e)
    {
        if ((sender as Button).Text == "test") 
        {
            await DisplayAlert("sorry", "try again later", "Ok");  
        }          
        await DisplayAlert("end", "end", "Ok");
    }

    //For ActivityIndicator, I use Task.Delay to make the effect clearer
    private async void Button_Clicked(object sender, EventArgs e)
    {
        ActivityIndicator indicator = new ActivityIndicator();
        indicator.IsRunning = true;
        indicator.IsVisible = true;
        indicator.Color = Color.Black;
        stacklayout.Children.Add(indicator);
        await Task.Delay(3000);
        indicator.IsRunning = false;
        indicator.IsVisible = false;
    }
}

Here is xaml code:这是xaml代码:

<StackLayout x:Name="stacklayout">
    <Button Text="test" 
            x:Name="btConnexion" 
            BackgroundColor="#AADC14" 
            Padding="0,0,0,0" 
            Clicked="btConnexion_Clicked"/>
    <Button Text="ActivityIndicator" Clicked="Button_Clicked"></Button>
</StackLayout>

Finally the problem was solved by putting the Activity Indicator at the end of the GridView, instead of the beginning, as below.最后通过将 Activity Indicator 放在 GridView 的末尾而不是开头来解决问题,如下所示。 So the AI was there, but not visible, because hidden in the background by the AbsoluteLayout.所以 AI 在那里,但不可见,因为被 AbsoluteLayout 隐藏在背景中。

</AbsoluteLayout>

<ActivityIndicator 
    IsRunning="{Binding IsBusy}"
    IsVisible="{Binding IsBusy}"
    Color="Red"
    VerticalOptions="CenterAndExpand" 
    HorizontalOptions="CenterAndExpand" 
    />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM