【问题标题】:Tell ActivityIndicator to show yourself and then navigate to another page告诉 ActivityIndi​​cator 展示自己,然后导航到另一个页面
【发布时间】:2016-03-03 15:17:46
【问题描述】:

Xamarin.Forms 项目中,我试图告诉ActivityIndicator 展示自己,然后导航到另一个页面

activityIndicatorObject = new ActivityIndicator()
                {
                    HorizontalOptions = LayoutOptions.CenterAndExpand,
                    Color = Color.Red,
                    IsVisible = false,
                    IsRunning = false,
                    IsEnabled = false,
                    BindingContext = this,
                };
ViewBookButton = new Button{ Text = "view book" };
ViewBookButton.Clicked += ViewBook;
protected void ViewBook(Object sender,EventArgs e)
            {
                //Go To View Book Page 
                activityIndicatorObject .IsVisible = true;
                activityIndicatorObject .IsRunning = true;
                activityIndicatorObject .IsEnabled = true;
                activityIndicatorObject .SetBinding(ActivityIndicator.IsVisibleProperty, "IsBusy");
                this.IsBusy = true;
                Navigation.PushAsync(new BookPage());
                this.IsBusy = false;
            }

ActivityIndicator 没有显示?如何让它显示?

【问题讨论】:

  • 值得注意的是,因为您正在加载一个将在UI Thread 上完成的新页面,此加载将阻止UI Thread,这意味着即使您确实获得了显示活动指示器它也不会旋转因为UI Thread会被页面加载阻塞

标签: c# xamarin xamarin.ios xamarin.forms portable-class-library


【解决方案1】:

据我了解,您告诉ActivityIndicator 展示自己,然后导航到另一个页面。并且新页面还没有拥有ActivityIndicator。我建议您在BookPageOnAppearing 方法中进行艰苦的操作。

protected override void OnAppearing()
{
    base.OnAppearing();

    activityIndicator.IsVisible = true;

    // Some hard operations

    activityIndicator.IsVisible = false;
}

或者使用绑定代替activityIndicator.IsVisible = true;

【讨论】:

  • 同意最好在“新”页面上显示 ActivityIndi​​cator。更好地直接推送新页面并在那里显示加载,而不是在未定义的时间推送页面。这会对用户产生误导,并且可能会给您带来难以调查的基于时间的错误。一般来说,尽量对用户输入做出反应——不要等待任何异步操作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多