应用程序忙时显示活动指示器

问题描述:

我正在开发带有Xamarin.Forms的跨平台移动应用程序。应用程序忙时显示活动指示器

我想通过一个简单的Activity Indicator和I按钮来模拟Web服务上的POST。我希望在按钮上单击活动指示器显示并保持,直到IsLoading属性设置为false。

这里我的XAML:

<AbsoluteLayout x:Name="layOuter"> 
    <ActivityIndicator x:Name="actWebRequest" 
     IsVisible="{ Binding IsLoading, 
          Mode=TwoWay }" 
     IsRunning="{ Binding IsLoading, 
          Mode=TwoWay }" 
     AbsoluteLayout.LayoutBounds="0.5, 0.5, 100, 100" 
     AbsoluteLayout.LayoutFlags="PositionProportional" /> 


    <Button x:Name="btnLogin" 
        IsVisible="{ Binding Show, 
          Mode=TwoWay }" 
        Text="{ StaticResource LoginButtonText }" 
        Command="{ Binding LoginCommand }" 
        BackgroundColor="Teal" /> 
</AbsoluteLayout> 

他的LoginCommand:

公共事件PropertyChangedEventHandler的PropertyChanged;

protected void OnPropertyChanged([CallerMemberName] string propertyName = null) 
    { 
     PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); 
    } 

    protected void SetValue<T>(ref T backingField, T value, [CallerMemberName] string propertyName = null) 
    { 
     if (EqualityComparer<T>.Default.Equals(backingField, value)) 
      return; 

     backingField = value; 

     OnPropertyChanged(propertyName); 
    } 

    private bool _isLoading; 
    private bool _show; 

    public bool IsLoading { get { return _isLoading; } set { SetValue(ref _isLoading, value); } } 
    public bool Show { get { return _show; } set { SetValue(ref _show, value); } } 

    private void Login() 
    { 
     IsLoading = true; 
     Show = false; 

     // DoSomething() 
     System.Threading.Tasks.Task.Delay(5000).Wait(); 

     IsLoading = false; 
     Show = true; 
    } 

通过点击登录按钮没有任何反应,并且5秒后应用程序将导航到另一个。我也试过ACR用户对话框,但它不起作用。

我的应用必须是Android和iOS兼容的。

你能帮我吗? 谢谢。

编辑:

是的,我绑定页面以WievModel这样:

public class ContentBase<T> : ContentPage where T : class, new() 
{ 
    public T ViewModel 
    { 
     get { return BindingContext as T; } 
     set { BindingContext = value; } 
    } 

    public ContentBase() { ViewModel = new T(); } 
} 

public class LoginPageBase : ContentBase<LoginViewModel> { } 

public partial class LoginPage : LoginPageBase 
{ 
    public LoginPage() 
    { 
     InitializeComponent(); 
    } 

    protected override bool OnBackButtonPressed() 
    { 
     return true; 
    } 
} 

public class LoginViewModel : ViewModelBase 
{ 
    private bool _isLoading; 
    private bool _show; 
    public bool IsLoading { get { return _isLoading; } set { SetValue(ref _isLoading, value); } } 
    public bool Show { get { return _show; } set { SetValue(ref _show, value); } } 
} 

我的意思是,当我点击登录按钮的活动指示灯不即使我展示将IsLoading设置为true。

+0

'点击登录按钮什么也没有发生,5秒后应用程序导航到另一个。 - 请详细说明。你设置了页面/视图的绑定上下文吗? – EvZ

+0

我添加了一些其他代码。关于导航;我的意思是这个页面是登录页面,所以在我的真实代码中,目前我评论了凭证检查过程,因为我只想看到活动指示符,在Login方法结束时,我已经'Application.Current.MainPage =新的NavigationPage(新HomePage())''所以我点击按钮,活动指标不显示,并在5秒后导航到HomePage。 –

+0

你为什么要绑定IsLoading TwoWay? –

的问题是显而易见的:

System.Threading.Tasks.Task.Delay(5000).Wait(); 

您*与Wait()主线程,等待它来代替:

await System.Threading.Tasks.Task.Delay(5000); 

那么你应该看到活动的指标。

+0

是的,你是对的。谢谢 –