1

我尝试绑定到 ImageView Alpha 属性,我创建了一个转换器来将此设置为布尔值。但我不认为设定值。

这是我的转换器

public class BooleanToOpacity : MvxValueConverter<bool,int>
{
    protected override int Convert(bool value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        var boolean = value as bool?;

        if (boolean.Value == true)
        {
            return 1;
        }
        else
        {
            return 127;
        }                
    }

    protected override bool ConvertBack(int value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (value > 127)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
}

我使用整数值,因为 Alpha 值是 0 到 255。

这是我绑定价值的路线。

   local:MvxBind="alpha TwitterPost, Converter=BooleanToOpacity" />

Mvx 跟踪我这个

   MvxBind:Warning: 11.56 Failed to create target binding for binding alpha for TwitterPost
   [0:] MvxBind:Warning: 11.56 Failed to create target binding for binding alpha for TwitterPost
   10-21 15:54:22.280 I/mono-stdout(12096): MvxBind:Warning: 11.56 Failed to create target binding for binding alpha for TwitterPost

任何想法?

提前致谢。

4

2 回答 2

2

There is no C# property available for "Alpha" in Xamarin.Android, so the Mvx framework doesn't know how to bind to it.

You could create a custom binding for this if you wanted to - something like:

    public class ImageViewAlphaTargetBinding : MvxAndroidTargetBinding
    {
        public ImageViewAlphaTargetBinding (ImageView target) : base(target)
        {
        }

        protected override void SetValueImpl(object target, object value)
        {
            var imageView = (ImageView)target;
            imageView.SetMyProperty((int)value);
        }

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

registered in Setup.cs with:

protected override void FillTargetFactories(Cirrious.MvvmCross.Binding.Bindings.Target.Construction.IMvxTargetBindingFactoryRegistry registry)
{
    registry.RegisterCustomBindingFactory<ImageView>(
                    "Alpha",
                    v=> new ImageViewAlphaTargetBinding (v) );
    base.FillTargetFactories(registry);
}

Alternatively, you could inherit from ImageView and provide a custom view that exposes an Alpha property.

For more on both these options, see:

于 2013-10-21T08:34:29.773 回答
1
<ImageView android:layout_width="match_parent"
        android:layout_height="match_parent"
        local:MvxBind="Alpha IsImageEnable , Converter = BoolToAlpha" />


public class BoolToAlphaValueConverter : BaseValueConverter<bool, Single>
    {
        protected override Single Convert(bool value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            Single alphavalue;
            if (value)
            {
                alphavalue = 1.0f;
            }
            else {
                alphavalue = 0.35f;
            }
            return alphavalue;
        }

    }
于 2017-02-21T12:52:52.347 回答