xamarin的UxBoolConvert
我正在做一个购物车应用程序,我需要更改addToCart按钮中的图像 我在列表视图中有“+”按钮,当我在购物车中添加项目时,图像必须更改为“-”。当我从购物车中删除一个项目时,图像再次变为“+”。我希望这里也需要我的通知,但如何在这里实现。我现在有一个布尔属性。我的逻辑是“在添加和删除时更改布尔值,但我不知道如何将图像更改为按钮” 这是我的视图模型xamarin的UxBoolConvert,xamarin,xamarin.forms,Xamarin,Xamarin.forms,我正在做一个购物车应用程序,我需要更改addToCart按钮中的图像 我在列表视图中有“+”按钮,当我在购物车中添加项目时,图像必须更改为“-”。当我从购物车中删除一个项目时,图像再次变为“+”。我希望这里也需要我的通知,但如何在这里实现。我现在有一个布尔属性。我的逻辑是“在添加和删除时更改布尔值,但我不知道如何将图像更改为按钮” 这是我的视图模型 public BaseViewModel(){ App.Instance.ViewModel = this; Tem
public BaseViewModel(){
App.Instance.ViewModel = this;
TempList = TempList ?? new ObservableCollection<cm_items>();
this.Title = AppResources.AppResource.Cart_menu_title;
this.IsContain = CartCell.buttonImg();
TempList.CollectionChanged += (sender, args) =>{
this.Price = CartCell.Calculate_price();};}
和转换器
public class BoolToImageConverter : IValueConverter
{
#region IValueConverter implementation
public object Convert (object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value is bool) {
if((bool)value == true)
return "add.png";
else
return "minus.png";
}
return "";
}
public object ConvertBack (object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException ();
}
#endregion
}
但是按钮中仍然没有图像您应该绑定这些属性,而不仅仅是分配它们:
yourImageView.SetBinding(Image.SourceProperty,v=>v.IsContain);
请记住为您的ViewModel实例设置Page
BindingContext
。而不是使用ValueConverter,为什么不使用一个将按钮的Image属性绑定到的Image属性呢
将一组示例代码放在一起,如下所示
即
公共类MyPage:ContentPage
{
ObservableCollection listToWatch=新的ObservableCollection();
公共MyPage()
{
Button ButtonUpdate=新建按钮{Text=“Image Button”,Image=“icon.png”};
按钮更新列表=新建按钮{Text=“将项目添加到列表”};
ButtonUpdate.SetBinding(Button.ImageProperty,新绑定(“ImagePath”);
更新列表。单击+=(发件人,e)=>{
Add(DateTime.Now.ToString());
};
listToWatch.CollectionChanged+=(发件人,e)=>{
如果(listToWatch.Count%2==1)
{
ImagePath=“noci.png”;
}
其他的
{
ImagePath=“icon.png”;
}
};
内容=新堆栈布局{
儿童={
按钮更新,
更新列表
}
};
Content.BindingContext=this;
}
私有字符串imagePath=“icon.png”;
公共字符串映像路径{
获取{return imagePath;}
集合{
imagePath=值;
OnPropertyChanged(“ImagePath”);
}
}
}
我把完整的应用程序扔了过去
视图模型看起来不错。请提供您的按钮好吗?如何将IsContain属性绑定到按钮的Image属性 在您的位置上,我将创建一个稍微不同的转换器:
public class UxBoolConvertExtension : IValueConverter, IMarkupExtension
{
public object OnTrue { get; set; }
public object OnFalse { get; set; }
public object OnNull { get; set; }
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return ImageSourceHelper.ToImageSourceIfPossible(this.ConvertValue(value), targetType);
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotSupportedException();
}
public object ProvideValue(IServiceProvider serviceProvider)
{
return this;
}
private object ConvertValue(object value)
{
if (value == null) return this.OnNull;
return (bool)value ? this.OnTrue : this.OnFalse;
}
}
并将按钮图像绑定到您的值上,如
<Button Image="{Binding Icon, Converter={m:UxBoolConvert OnTrue='minus.png', OnFalse='add.png'}}" />
在我的视图单元格中
var addedOrNotImageBinding = new Binding("IsAddedToCart", 0, new ImagePathConvertor());
btn_Cart.SetBinding(ImageButton.SourceProperty,addedOrNotImageBinding);
btn_Cart.Clicked += (sender, e ) =>
{
sender = BindingContext;
cm_items item = (cm_items)sender;
//ViewCell viewCell = (ViewCell)sender;
//BaseViewModel item = (BaseViewModel)viewCell.BindingContext;
if (item.IsAddedToCart)
{
item.IsAddedToCart = false;
App.Instance.ViewModel.TempList.Remove(BindingContext as cm_items);
}
else
{
item.IsAddedToCart = true;
App.Instance.ViewModel.TempList.Add(BindingContext as cm_items);
}
// CartCell.Calculate_price();
};
并且必须添加一个名为ImagePathConvertor的类来转换图像
public class ImagePathConvertor : IValueConverter
{
public ImagePathConvertor() { }
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if ((bool)value)
{
return "minus";
}
else
{
return "add";
}
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
#endregion
}
在我的模型中
private bool isAddedToCart;
public bool IsAddedToCart
{
get { return isAddedToCart; }
set
{
isAddedToCart = value;
OnPropertyChanged("IsAddedToCart");
}
}
private void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
这对我来说效果很好图像正在改变谢谢@David@Jauhenka这是我的按钮代码,在视图中是cellbtn_Cart=newimagebutton{Source=image1,BackgroundColor=Color.Transparent,VerticalOptions=LayoutOptions.CenterAndExpand,HorizontalOptions=LayoutOptions.CenterAndExpand,};btn_Cart.SetBinding(ImageButton.SourceProperty,“IsContain”,BindingMode.Default,new booltimageconverter());我不使用xaml,而是使用C#,我的逻辑是在将项目添加到购物车时,将is Contain属性更改为false,并在再次从购物车中删除项目时更改图像名称。is Contain属性再次更改为true,它将变为true。“如何将IsContain属性绑定到按钮的图像属性?”我在xamarin表单中的一些FRND帮助我实现了这一点,我将把讨论的链接放好,我看到的应该是有效的,除了一件事。为什么使用ImageButton.SourceProperty而不是button.ImageProperty?这也是一个可绑定属性吗?
var addedOrNotImageBinding = new Binding("IsAddedToCart", 0, new ImagePathConvertor());
btn_Cart.SetBinding(ImageButton.SourceProperty,addedOrNotImageBinding);
btn_Cart.Clicked += (sender, e ) =>
{
sender = BindingContext;
cm_items item = (cm_items)sender;
//ViewCell viewCell = (ViewCell)sender;
//BaseViewModel item = (BaseViewModel)viewCell.BindingContext;
if (item.IsAddedToCart)
{
item.IsAddedToCart = false;
App.Instance.ViewModel.TempList.Remove(BindingContext as cm_items);
}
else
{
item.IsAddedToCart = true;
App.Instance.ViewModel.TempList.Add(BindingContext as cm_items);
}
// CartCell.Calculate_price();
};
public class ImagePathConvertor : IValueConverter
{
public ImagePathConvertor() { }
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if ((bool)value)
{
return "minus";
}
else
{
return "add";
}
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
#endregion
}
private bool isAddedToCart;
public bool IsAddedToCart
{
get { return isAddedToCart; }
set
{
isAddedToCart = value;
OnPropertyChanged("IsAddedToCart");
}
}
private void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}