문제

나는 사용하고있다 MvxBindableListView 바인딩하다 List<> 데이터 개체를 ListView.행에 사용하는 레이아웃에는 여러 가지가 있습니다. TextView에스.성공적으로 바인딩 중입니다. Text 이들 각각에 대한 속성을 내 데이터 개체의 속성에 바인딩할 수 없다는 것을 발견했습니다. TextColor Android용 Mono에는 해당 속성이 존재하지 않기 때문입니다. TextView에스;대신에 당신은 SetTextColor() 방법.그렇다면 데이터 개체 속성을 메서드에 어떻게 바인딩할 수 있나요?다음은 내가 사용하려고 시도한 코드입니다.

    <TextView
        android:id="@+id/MyValueTextView"
        android:layout_width="50dp"
        android:layout_height="20dp"
        android:layout_gravity="right"
        android:gravity="center_vertical|right"
        android:textSize="12sp"
        local:MvxBind="
        {
          'Text':{'Path':'MyValue','Converter':'MyValueConverter'},
          'TextColor':{'Path':'MyOtherValue','Converter':'MyOtherConverter'}
        }" />
도움이 되었습니까?

해결책

Conference 샘플에는 "IsFavorite"에 대한 사용자 정의 양방향 바인딩을 추가하는 예가 있습니다. 다음을 참조하세요.

이 예는 다음에서 좀 더 자세히 설명됩니다. Android의 MVVMCross 바인딩

단방향 "소스-대상" 사용자 정의 바인딩의 경우 코드는 좀 더 간단해야 합니다. SetValue - 호출할 필요가 없습니다. FireValueChanged 모든 이벤트 처리 코드에서.


textColor의 경우 바인딩이 다음과 같이 보일 것이라고 생각합니다.

public class MyCustomBinding
    : MvxBaseAndroidTargetBinding
{
    private readonly TextView _textView;

    public MyCustomBinding(TextView textView)
    {
        _textView = textView;
    }

    public override void SetValue(object value)
    {
        var colorValue = (Color)value;
        _textView.SetTextColor(colorValue);
    }

    public override Type TargetType
    {
        get { return typeof(Color); }
    }

    public override MvxBindingMode DefaultMode
    {
        get { return MvxBindingMode.OneWay; }
    }
}

다음과 같이 설정됩니다.

    protected override void FillTargetFactories(MvvmCross.Binding.Interfaces.Bindings.Target.Construction.IMvxTargetBindingFactoryRegistry registry)
    {
        base.FillTargetFactories(registry);

        registry.RegisterFactory(new MvxCustomBindingFactory<TextView>("TextColor", (textView) => new MyCustomBinding(textView)));
    }

메모:저는 이 예제 코드를 컴파일하지 않았습니다. 작동하게 되면 다시 돌아와서 이 의사 코드를 수정하세요. :)

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top