【发布时间】:2021-12-06 05:10:48
【问题描述】:
我正在编写一个小型 Xamarin.Forms 应用程序,并尝试执行一些操作,例如显示警报或显示活动指示器。但是,我在代码中编写的所有内容都只在方法的末尾执行,甚至是 Activity Indicator,这当然不是目标。不同的警报甚至会显示在 LIFO 中,因此代码中的最后一个会首先出现。
这是 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");
}
我在 .xaml 中调用它的地方:
<Button Text="{x:Static resource:AppResources.Login}"
x:Name="btConnexion"
BackgroundColor="#AADC14"
Padding="0,0,0,0"
Clicked="btConnexion_Clicked"/>
我在methodd中放了一些alerts,显示“In the try”、“Beginning”等。如果我一步步调试代码,所有的alerts只会在方法结束时执行一次,而是在编译期间。而在 FILO 中,第一个是“After finally”,然后是“Finally”等。而且活动指示器甚至没有显示,原因与我想的相同。
我不知道它是否会改变任何东西,但是 xaml 必须按照 xaml.cs 中的编写方式进行编译:
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class LoginPage : ContentPage
【问题讨论】:
-
在 DisplayAlert 之前放置等待
标签: c# xaml xamarin compilation activity-indicator