0
votes

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

1
Generally, using data binding is the "right" way to update a view in Xamarin.Forms. You need to bind Text property of the label in your view to some property of your viewmodel and then when you update viewmodel's property from sqlite, the text will be updated. You probably can manage this with message subscriptions, but I think it will mess your code and anyway, this is not a recommended wayGrisha
@Grisha I have updated my post please have a look ,i think i can use i notify for this problem . But view is not updating. or is it possible to use a message sender for this solution ?user4318551
What is the definition of TotalAmount property in your ViewModel? Is it property or field? Does it have OnPropertyChanged call inside it?Grisha
If it is easier and quick solution, you can use the messaging center developer.xamarin.com/guides/cross-platform/xamarin-forms/…Mario Galván
Total Amount is a property i have OnPropertyChanged , i can see i have been update my post @Grishauser4318551

1 Answers

0
votes

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