简体   繁体   中英

What is the function of Message sender in xamarin.forms

What is the function of Message sender in xamarin.forms? In my app I have cart contain list view and a Grant Total label. Is it possible to update the label using message sender? I can get the total amount from my sqlite db I need to update it to the view.

This is my number picker index change event in view cell

numPicker.SelectedIndexChanged += (sender, args) =>
{
    //  var price = _cartQuery.GetSum();
    sender = BindingContext;
    //    cm_items item = (cm_items)sender;
    if(Int32.Parse(btn_NumBtn.Text)<=1)
    {
        lbl_Price.Text = ((numPicker.SelectedIndex + 1) * (Int32.Parse(lbl_Price.Text))).ToString();
        btn_NumBtn.Text = (numPicker.SelectedIndex + 1).ToString();
    }
    else
    {
        int a = Int32.Parse(lbl_Price.Text);
        int b = Int32.Parse(btn_NumBtn.Text);
        int c = a / b;
        lbl_Price.Text = ((numPicker.SelectedIndex + 1) * c).ToString();
        btn_NumBtn.Text = (numPicker.SelectedIndex + 1).ToString();
    }
    _cartQuery.UpdatePicker((BindingContext as CartDB).Cart_Item_Id, numPicker.SelectedIndex + 1, Int32.Parse(lbl_Price.Text));
    price = _cartQuery.GetSum();
    //  App.Instance.ViewModel.TotalAmount = price;
    //   _cartDB.total = App.Instance.ViewModel.TotalAmount;
    Calculate_price();

    numPicker.IsEnabled = false;
};

Calculate_price method

public double Calculate_price()
{
    try
    {
        var price = 0;
        price = _cartQuery.GetSum();
        App.Instance.ViewModel.TotalAmount = price;
        return price;
    }
    catch (Exception ex)
    {
        throw ex;
    }
}

In my view i have a label named grant total, i need to update the total on e number picker change

Label lbl_amnt = new Label
{
    //  Text = viewModel.Price.ToString(),
    // Text=CartCell.price.ToString(),
    Text = price.ToString(),
    FontSize = 18,
    FontAttributes = FontAttributes.Bold,
    TextColor = Color.FromRgb(102, 204, 102),
    VerticalOptions = LayoutOptions.CenterAndExpand,
    HorizontalOptions = LayoutOptions.EndAndExpand,
};
lbl_amnt.SetBinding(Label.TextProperty, "TotalAmount");

update to my post as per the comment from @Grish

In my view model i have this TotalAmount as a property

public double _TotalAmount;
public double TotalAmount
{
    get { return _TotalAmount; }
    set { _TotalAmount = value; OnPropertyChanged("TotalAmount");}
}

I think the better solution is i notify but the thing is view is not binding

Binding is definitely the answer in your case. I think the problem is that you bind string (label's text) to property of type double . You should specify IValueConverter or stringFormat parameters in your call to SetBinding . Check this link:https://forums.xamarin.com/discussion/19146/binding-to-integers

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM