本文介绍了如何在屏幕中间显示ActivityIndi​​cator?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个活动指示器并将其添加到StackLayout中,当我使其运行时,在模拟器中它会显示在右上角Android 4.4中,而在iOS中则没有显示,而在Android 6手机中则不显示

I've created an activity indicator and added it to StackLayout and when I make it running, in the emulator it shows in the top right corner Android 4.4 and in iOS no show and in Android 6 phone, it don'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);

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

I want to show the activity indicator to the center of the screen because the operation takes time to complete.

推荐答案

在状态栏中看到的指示符是 IsBusy 属性。您的代码无法正常运行的原因是,您试图将 ActivityIndi​​cator 的可见性绑定到该属性-但您未指定绑定源。如果查看调试器的应用程序输出日志,则可能会看到在类型'Object'上找不到属性'IsBusy'的消息。

The indicator that you are seeing in the status bar is the default behavior of the IsBusy property of the base page class. The reason your code isn't working is because you are attempting to bind visibility of your ActivityIndicator to that property - but you aren't specifying a binding source. If you look in your debugger's application output log then you will probably see messages along the lines of "Property 'IsBusy' not found on type 'Object'".

要修复它,您只需要将每个绑定的Binding Context指向表单。试试看:

To fix it, you simply need to point the Binding Context of each binding to the form. Give this a try:

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;
    }
}

这篇关于如何在屏幕中间显示ActivityIndi​​cator?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 09:57