在代码中编写自定义控件时如何在Bindable属性之间设置绑定?

时间:2017-12-19 09:59:37

标签: c# xaml xamarin mvvm xamarin.forms

我正在尝试将此控件转换为C#代码:

<AbsoluteLayout IsVisible="{Binding IsBusy}" IsEnabled="{Binding IsBusy}">
                    <ContentView x:Name="actIndBackground"
                 BackgroundColor="#AC000000"
                 AbsoluteLayout.LayoutFlags="All"
                 AbsoluteLayout.LayoutBounds="0,0,1,1"
                 IsVisible="{Binding IsBusy}"
                 IsEnabled="{Binding IsBusy}">
                    </ContentView>
                    <ActivityIndicator x:Name="actInd"
                       AbsoluteLayout.LayoutFlags="PositionProportional"
                       AbsoluteLayout.LayoutBounds="0.5,0.5,-1,-1"
                       IsRunning="{Binding IsBusy}"
                       IsVisible="{Binding IsBusy}"
                       IsEnabled="{Binding IsBusy}"/>
                </AbsoluteLayout>

我的主要问题是如何将AbsoluteLayout,ActivityIndi​​cator和ContentView的属性绑定到视图模型的IsBusy属性,

我尝试了这个,但不起作用:

public class Loader : ContentView
    {
        AbsoluteLayout layout;
        public static readonly BindableProperty IsActiveProperty = BindableProperty.Create(nameof(IsActive), typeof(bool), typeof(Loader), defaultValue: false);
        public bool IsActive
        {
            get { return (bool)GetValue(IsActiveProperty); }
            set { SetValue(IsActiveProperty, value); }
        }


        public Loader()
        {
            this.SetBinding(IsVisibleProperty, new Binding(nameof(IsActive)));
            this.SetBinding(IsEnabledProperty, new Binding(nameof(IsActive)));
            BindingContext = this;

            ContentView actIndBackground = new ContentView
            {
                BackgroundColor = Color.FromHex("#AC000000"),
            };
            actIndBackground.SetBinding(ContentView.IsVisibleProperty, new Binding(nameof(IsActive)));
            actIndBackground.SetBinding(ContentView.IsEnabledProperty, new Binding(nameof(IsActive)));

            layout = new AbsoluteLayout();
            layout.Children.Add(actIndBackground);
            AbsoluteLayout.SetLayoutBounds(actIndBackground, new Rectangle(0, 0, 1, 1));
            AbsoluteLayout.SetLayoutFlags(actIndBackground, AbsoluteLayoutFlags.All);

            ActivityIndicator actInd = new ActivityIndicator
            {
                IsRunning = IsActive,
                IsVisible = IsActive,
                IsEnabled = IsActive
            };
            actInd.SetBinding(ActivityIndicator.IsRunningProperty, new Binding(nameof(IsActive)));
            actInd.SetBinding(ActivityIndicator.IsVisibleProperty, new Binding(nameof(IsActive)));
            actInd.SetBinding(ActivityIndicator.IsEnabledProperty, new Binding(nameof(IsActive)));

            AbsoluteLayout.SetLayoutBounds(actInd, new Rectangle(0.5, 0.5, -1, -1));
            AbsoluteLayout.SetLayoutFlags(actInd, AbsoluteLayoutFlags.PositionProportional);
            layout.Children.Add(actInd);
            Content = layout;
        }
    }

想要像这样消费它

<controls:Loader IsActive="{Binding IsBusy}"/>

0 个答案:

没有答案