xamarin 中的 UxBoolConvert [英] UxBoolConvert in xamarin

查看:31
本文介绍了xamarin 中的 UxBoolConvert的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个购物车应用程序,我需要更改 addToCart 按钮中的图像我在列表视图中有+"按钮,而我将一个项目添加到购物车中,图像必须更改为-".当我从我的购物车中删除一个项目时,图像再次更改为+".我希望在这里我也需要通知但如何在此处实施.我现在有一个布尔属性.我的逻辑是在添加和删除时更改布尔值,但我不知道如何将图像更改为按钮"这是我的视图模型

i'm doing a cart app i need to change the image in the addToCart button I have"+" button in list view while i add an item in to cart the image must change to"-". when i delete an item from my cart the image again change to"+".I hope here also i need i notify but how to implement here .I have a Boolean property now. My logic is this "change the Boolean value while adding and removing but i don't know how to change the image to the button" this is my view model

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();};}

和我的财产

  private bool _isContain;
    public bool IsContain
    {
        get { return _isContain; }
        set {_isContain=value;OnPropertyChanged("IsContain");}
    }

和转换器

 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
}

但我仍然没有按钮中的图像

but still i have no image in the button

推荐答案

在我的视图单元格中

  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 的类用于转换图像

and have to add a class named ImagePathConvertor used to convert the image

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

it works nicely for me image is changing thanks @David

这篇关于xamarin 中的 UxBoolConvert的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆