绑定阿尔法安卓
本文关键字:阿尔法 绑定 | 更新日期: 2023-09-27 18:36:22
我尝试绑定到 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
知道吗?
提前谢谢。
Xamarin.Android 中没有可用于"Alpha"的 C# 属性,因此 Mvx 框架不知道如何绑定到它。
如果需要,可以为此创建自定义绑定 - 如下所示:
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); }
}
}
在安装程序中注册.cs具有:
protected override void FillTargetFactories(Cirrious.MvvmCross.Binding.Bindings.Target.Construction.IMvxTargetBindingFactoryRegistry registry)
{
registry.RegisterCustomBindingFactory<ImageView>(
"Alpha",
v=> new ImageViewAlphaTargetBinding (v) );
base.FillTargetFactories(registry);
}
或者,您可以从 ImageView 继承并提供公开 Alpha
属性的自定义视图。
有关这两个选项的详细信息,请参阅:
- 自定义绑定 http://slodge.blogspot.co.uk/2013/06/n28-custom-bindings-n1-days-of-mvvmcross.html
- 继承以添加属性 - http://slodge.blogspot.co.uk/2013/07/n33-animating-data-bound-text-changes.html
<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;
}
}