2017-04-01 42 views
1

我正在開發帶有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? –

回答

1

的問題是顯而易見的:

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

您封鎖與Wait()主線程,等待它來代替:

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

那麼你應該看到活動的指標。

+0

是的,你是對的。謝謝 –