MVVMCross将属性绑定到子视图android

时间:2016-12-19 23:22:26

标签: xamarin binding xamarin.android mvvmcross

我有一个从MvxFragment扩展的Fragment类。 该类从Resources获取片段视图,并在主视图中对其进行扩充。

public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
            base.OnCreateView(inflater, container, savedInstanceState);
            var view = this.BindingInflate(Resource.Layout.ControlFragmentView, null);

            var chargeList = view.FindViewById<MvxListView>(Resource.Id.ControlChargeListView);
            chargeList.Divider = null;
            chargeList.Adapter = new MvxListElementAdapter(container.Context, (IMvxAndroidBindingContext)BindingContext, Mvx.GetSingleton<IThemeService>().ThemeViewControlChargeRecordListElement, typeof(ControlChargeRecordListElement));

            return view;
        }

这是我的ControlFragmentView.axml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:local="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <MyApp.Droid.Controls.Control.ControlIndicator
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        local:MvxBind="ChargeValue ChargeValue;"
        />
</LinearLayout>

这是我的ViewModel

public class ControlViewModel : MvxViewModel
{
     private float _chargeValue;
        public float ChargeValue
        {
            get { return _chargeValue; }
            set
            { 
                _chargeValue = value;
                RaisePropertyChanged(() => ChargeValue);
            }
        }
}

这是我的MyApp.Droid.Controls.Control.ControlIndicator类

public class ControlIndicator : FrameLayout
{
    private float _chargeValue;
    public float ChargeValue
    {
        get { return _chargeValue; }
        set
        {
            _chargeValue = value;
        }
    }

    public ControlChargeIndicator(Context context, IAttributeSet attrs)
            : base(context)
    {
        Initialize();
    }

    private function Initialize()
    {
        var bar = new ControlBar(Context);
        AddView(bar);

        var bar2 = new ControlBar(Context);
        AddView(bar2);

        ....

        var barN = new ControlBar(Context);
        AddView(barN);
    }
}

这是我的ControlBar类

public class ControlBar : FrameLayout
{
    private float _chargeValue;
    public float ChargeValue
    {
        get { return _chargeValue; }
        set
        {
            _chargeValue = value;
        }
    }
}

如何让ControlBar子视图知道ChargeValue已被更改?我可以检测何时在ControlIndicator类中更改ChargeValue但在ControlBar类中没有。我怎样才能实现它?

1 个答案:

答案 0 :(得分:0)

您尚未将ChargeValue课程中的ControlIndicator媒体资源与ControlBar ChargeValue相关联。为什么你甚至有这种嵌套?

尝试更改:

private float _chargeValue;
public float ChargeValue
{
    get { return _chargeValue; }
    set
    {
        _chargeValue = value;
    }
}

要:

public float ChargeValue
{
    get { return _bar.ChargeValue; }
    set
    {
        _bar.ChargeValue = value;
    }
}

确保_barControlBar的实例作为成员变量。