简体   繁体   English

xamarin.forms 中 Message sender 的作用是什么

[英]What is the function of Message sender in xamarin.forms

What is the function of Message sender in xamarin.forms? xamarin.forms 中 Message sender 的作用是什么? In my app I have cart contain list view and a Grant Total label.在我的应用程序中,我的购物车包含列表视图和 Grant Total 标签。 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.我可以从我需要将它更新到视图的 sqlite 数据库中获取总量。

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在我看来,我有一个名为 grant total 的标签,我需要更新 e 号码选择器更改的总数

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根据@Grish 的评论更新我的帖子

In my view model i have this TotalAmount as a property在我的视图模型中,我将此 TotalAmount 作为属性

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 .我认为问题在于您将string (标签的文本)绑定到double类型的属性。 You should specify IValueConverter or stringFormat parameters in your call to SetBinding .您应该在调用SetBinding指定IValueConverterstringFormat参数。 Check this link:https://forums.xamarin.com/discussion/19146/binding-to-integers检查此链接:https ://forums.xamarin.com/discussion/19146/binding-to-integers

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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