如何在屏幕中间显示ActivityIndi​​cator?

时间:2016-03-23 11:36:41

标签: xamarin xamarin.forms activity-indicator

我已经创建了一个活动指示器并将其添加到StackLayout,当我让它运行时,在模拟器中它显示在右上角的Android 4.4和iOS中没有显示,在Android 6手机中,它不会&# 39; t show。

var indicator = new ActivityIndicator()
            {
                Color = Color.Blue,
            };
            indicator.SetBinding(ActivityIndicator.IsVisibleProperty, "IsBusy", BindingMode.OneWay);
            indicator.SetBinding(ActivityIndicator.IsRunningProperty, "IsBusy", BindingMode.OneWay);
AbsoluteLayout.SetLayoutFlags(indicator, AbsoluteLayoutFlags.PositionProportional);
            AbsoluteLayout.SetLayoutBounds(indicator, new Rectangle(0.5, 0.5, AbsoluteLayout.AutoSize, AbsoluteLayout.AutoSize));
mainLayout.Children.Add(indicator);

我想将活动指示器显示在屏幕中央,因为操作需要一些时间才能完成。

2 个答案:

答案 0 :(得分:8)

您在状态栏中看到的指示符是基页类的IsBusy属性的默认行为。您的代码无法正常工作的原因是您尝试将ActivityIndicator的可见性绑定到该属性 - 但您没有指定绑定源。如果您查看调试器的应用程序输出日志,那么您可能会看到类型为'Object'时未找到“Property'IsBusy'行的消息。”

要修复它,您只需要将每个绑定的绑定上下文指向表单。试一试:

public partial class App : Application
{
    public App ()
    {
        var mainLayout = new AbsoluteLayout ();
        MainPage = new ContentPage {
            Content = mainLayout
        };

        var containerPage = Application.Current.MainPage;

        var indicator = new ActivityIndicator() {
            Color = Color.Blue,
        };
        indicator.SetBinding(VisualElement.IsVisibleProperty, new Binding("IsBusy", BindingMode.OneWay, source: containerPage));
        indicator.SetBinding(ActivityIndicator.IsRunningProperty, new Binding("IsBusy", BindingMode.OneWay, source: containerPage));
        AbsoluteLayout.SetLayoutFlags(indicator, AbsoluteLayoutFlags.PositionProportional);
        AbsoluteLayout.SetLayoutBounds(indicator, new Rectangle(0.5, 0.5, AbsoluteLayout.AutoSize, AbsoluteLayout.AutoSize));
        mainLayout.Children.Add(indicator);

        containerPage.IsBusy = true;
    }
}

答案 1 :(得分:0)

您将活动指示符添加到堆栈布局,但是您设置的是绝对布局LayoutFlags,它们不会起作用。

能够实现你想要的,你需要吸吮结构

AbsoluteLayout
        StackLayout
        ActivityIndicator

mainLayout应该是AbsoluteLayout,所有内容都应该包含在嵌套的StackLayout中。