活动指标在Xamarin.Forms中不起作用

时间:2018-02-12 11:20:07

标签: xamarin xamarin.forms

我在绝对布局中设计了一个活动指示器。基于按钮单击事件,我尝试交替显示和隐藏活动指示器。但由于某种原因,我看不到我的活动指标。任何帮助将不胜感激!!!提前谢谢。

这是我的.xaml.cs类:

public partial class PBTestPage : ContentPage
{
    private bool _pbIndicator;
    public PBTestPage()
    {
        InitializeComponent();
    }

    public bool PBIndicator{
        get{
            return _pbIndicator;
        }set{
            _pbIndicator = value;
            OnPropertyChanged();
        }
    }

    protected override void OnAppearing()
    {
        base.OnAppearing();
        var parentLayout = new AbsoluteLayout();

        var stackContent = new StackLayout();
        AbsoluteLayout.SetLayoutFlags(stackContent,AbsoluteLayoutFlags.PositionProportional);
        AbsoluteLayout.SetLayoutBounds(stackContent,new Rectangle(0f,0f,AbsoluteLayout.AutoSize,AbsoluteLayout.AutoSize));

        var activityIndicator = new ActivityIndicator
        {
            Color = Color.Black,
            IsRunning = PBIndicator,
            IsVisible = PBIndicator
        };
        AbsoluteLayout.SetLayoutFlags(activityIndicator, AbsoluteLayoutFlags.PositionProportional);
        AbsoluteLayout.SetLayoutBounds(activityIndicator, new Rectangle(.5, .5, AbsoluteLayout.AutoSize, AbsoluteLayout.AutoSize));

        var button = new Button
        {
            Text="Click",
            VerticalOptions=LayoutOptions.CenterAndExpand,
            HorizontalOptions=LayoutOptions.CenterAndExpand,
        };
        button.Clicked += OnClicked;

        stackContent.Children.Add(button);

        parentLayout.Children.Add(stackContent);
        parentLayout.Children.Add(activityIndicator);

        Content = parentLayout;
    }

    private void OnClicked(object sender, EventArgs e)
    {
        if(PBIndicator==false){
            PBIndicator = true;
        }else{
            PBIndicator = false;
        }

    }
}

2 个答案:

答案 0 :(得分:1)

我推断您打算使用OnPropertyChanged来使用绑定,所以现在是开始这样做的好时机。

我已经对您的代码进行了一些更改,我猜它现在可以正常运行了。变化是:

  1. 将布局创建移动到构造函数(我无法在每次页面显示时创建相同的布局作为一个好选择);
  2. OnClicked事件只是反转属性的值,不需要在使用if之前检查它;
  3. 使用Bindings处理ActivityIndi​​cator的属性状态;
  4. true事件中将PBIndicator设置为OnAppearing属性。
  5. 这是更改的代码:

    public partial class PBTestPage : ContentPage
    {
        private bool _pbIndicator;
        public bool PBIndicator
        {
            get { return _pbIndicator; }
            set
            {
                _pbIndicator = value;
                OnPropertyChanged();
            }
        }
    
        public PBTestPage()
        {
            InitializeComponent();
    
            var parentLayout = new AbsoluteLayout();
    
            var stackContent = new StackLayout();
            AbsoluteLayout.SetLayoutFlags(stackContent, AbsoluteLayoutFlags.PositionProportional);
            AbsoluteLayout.SetLayoutBounds(stackContent, new Rectangle(0f, 0f, AbsoluteLayout.AutoSize, AbsoluteLayout.AutoSize));
    
            var activityIndicator = new ActivityIndicator
            {
                Color = Color.Black
            };
    
            activityIndicator.SetBinding(ActivityIndicator.IsRunningProperty, new Binding(nameof(PBIndicator)));
            activityIndicator.SetBinding(ActivityIndicator.IsVisibleProperty, new Binding(nameof(PBIndicator)));
            activityIndicator.BindingContext = this;
    
            AbsoluteLayout.SetLayoutFlags(activityIndicator, AbsoluteLayoutFlags.PositionProportional);
            AbsoluteLayout.SetLayoutBounds(activityIndicator, new Rectangle(.5, .5, AbsoluteLayout.AutoSize, AbsoluteLayout.AutoSize));
    
            var button = new Button
            {
                Text = "Click",
                VerticalOptions = LayoutOptions.CenterAndExpand,
                HorizontalOptions = LayoutOptions.CenterAndExpand,
            };
            button.Clicked += OnClicked;
    
            stackContent.Children.Add(button);
    
            parentLayout.Children.Add(stackContent);
            parentLayout.Children.Add(activityIndicator);
    
            Content = parentLayout;
        }
    
        protected override void OnAppearing()
        {
            base.OnAppearing();
            PBIndicator = true;
        }
    
        private void OnClicked(object sender, EventArgs e)
        {
            PBIndicator = !PBIndicator;
        }
    }
    

    让我知道它是否有效。我希望它有所帮助。

答案 1 :(得分:0)

试试这个

{{1}}