应用忙时显示 Activity 指示器
Display Activity Indicator while app is busy
我正在使用 Xamarin.Forms 开发跨平台移动应用程序。
我正在尝试使用简单的 Activity 指示器和 I 按钮在 Web 服务上模拟 POST。我希望在按钮上单击 Activity 指示器显示并保持直到 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>
他是登录命令:
public 事件 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,Activity 指示器也不会显示。
问题很明显:
System.Threading.Tasks.Task.Delay(5000).Wait();
您正在使用 Wait()
阻塞主线程,改为等待它:
await System.Threading.Tasks.Task.Delay(5000);
然后您应该会看到 activity 指标。
我正在使用 Xamarin.Forms 开发跨平台移动应用程序。
我正在尝试使用简单的 Activity 指示器和 I 按钮在 Web 服务上模拟 POST。我希望在按钮上单击 Activity 指示器显示并保持直到 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>
他是登录命令:
public 事件 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,Activity 指示器也不会显示。
问题很明显:
System.Threading.Tasks.Task.Delay(5000).Wait();
您正在使用 Wait()
阻塞主线程,改为等待它:
await System.Threading.Tasks.Task.Delay(5000);
然后您应该会看到 activity 指标。