简体   繁体   English

将标签的文本绑定到 Xamarin forms c# 中的变量

[英]Binding Label's Text to a Variable in Xamarin forms c#

So Im trying to creat a simple app like a shopping app.所以我试图创建一个简单的应用程序,比如购物应用程序。 so I have categories and multiple items for each category, and when you get to choose an item then you will have the posibility to increase how many you need or delete the item.所以我有类别和每个类别的多个项目,当您选择一个项目时,您将有可能增加您需要的项目数量或删除该项目。 For exemple I chosed three items, so my cart have 3 items where each one have an Add button and a delete button.例如,我选择了三个项目,所以我的购物车有 3 个项目,每个项目都有一个添加按钮和一个删除按钮。 When I hit the add button the number of the items shown should increase and so on.当我点击添加按钮时,显示的项目数量应该会增加,依此类推。

so what I've done so far is creating a JSON file that having all my categories, and once I hit a category I get to deserialize another JSON file that have all my items, so the items shown depends on the category I chosed of course.所以到目前为止我所做的是创建一个包含我所有类别的 JSON 文件,一旦我点击一个类别,我就会反序列化另一个包含我所有项目的 JSON 文件,所以显示的项目当然取决于我选择的类别.

Now each time i choose an item it get added to the cart and shown on the bottom page with a + and - buttons and so on.现在,每次我选择一个项目时,它都会被添加到购物车中,并显示在底部页面上,并带有 + 和 - 按钮等等。

so I created a category class to deserialize my json, and an objets class to deserialize my Item's json.所以我创建了一个类别 class 来反序列化我的 json,并创建一个对象 class 来反序列化我的项目的 Z46436DEEC76ECDF3524DZ1 I implememted the INotifyChangedProperty in the objets class so that I can keep showin whenever the number of a chosen item get increased, so basicly thats my ViewModel, but I guess that it's like that I need a ViewModel of each created item?我在对象 class 中实现了 INotifyChangedProperty,这样每当所选项目的数量增加时我就可以继续显示,所以基本上这就是我的 ViewModel,但我想这就像我需要每个创建项目的 ViewModel? so I guess what I really need to use is the ObservableCollection..所以我想我真正需要使用的是 ObservableCollection..

I hope I explained everything well, and waiting for your feedbacks about if Im doing it right or wrong and how should i proceed to get what I want.我希望我能很好地解释一切,并等待您对我做对或错以及我应该如何继续得到我想要的东西的反馈。 thank you so much太感谢了

the problems is that to set the bindingcontext to my "Objets" Class I have to put the arguments in it, and then my Label well get a precised value... what should I do?问题是要将绑定上下文设置为我的“Objets”Class 我必须将 arguments 放入其中,然后我的 Label 会得到一个精确的值...我应该怎么做?

I do one sample about your model, you can take a look:我做了一个关于你的 model 的样品,你可以看看:

<ContentPage.Content>
    <StackLayout>
        <Label x:Name="label1" />

        <Button
            x:Name="btn1"
            Clicked="Btn1_Clicked"
            Text="change value" />
    </StackLayout>
</ContentPage.Content>


public partial class Page15 : ContentPage
{
    public Objets model { get; set; }
    public Page15()
    {
        InitializeComponent();
        model= new Objets("test 1", 1.001f, " test11111", 12);
        this.BindingContext = model;
        label1.SetBinding(Label.TextProperty, "nbr_objet");
    }

    private void Btn1_Clicked(object sender, EventArgs e)
    {
        model.nbr_objet = 20;
    }
}
public class Objets : INotifyPropertyChanged
{
    public string Designation { get; set; }
    public float Prix { get; set; }
    public string imageUrl { get; set; }
    private int Nbr_Objet;

    public int nbr_objet
    {
        get { return Nbr_Objet; }
        set
        {
            Nbr_Objet = value;
            RaisePropertyChanged("nbr_objet");
        }
    }



    public Objets(string Designation, float Prix, string imageUrl, int Nbr_Objet)
    {
        this.Designation = Designation;
        this.Prix = Prix;
        this.imageUrl = imageUrl;
        this.Nbr_Objet = Nbr_Objet;
    }

    public event PropertyChangedEventHandler PropertyChanged;


    public void RaisePropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

Update:更新:

but I guess that it's like that I need a ViewModel of each created item?但我想这就像我需要每个创建项目的 ViewModel 一样? so I guess what I really need to use is the ObservableCollection..所以我想我真正需要使用的是 ObservableCollection..

You said that you have three categories, and each category have many items, If you display these in ListView, category is used as Group header, and I suggest you can use the same model for different item for different categories, then add in Observablecollection, because it have implemented INotifyPropertyChanged interface.你说你有三个分类,每个分类有很多项目,如果你在ListView中显示这些,分类被用作组header,我建议你可以为不同的分类使用相同的model,然后添加Observablecollection,因为它已经实现了 INotifyPropertyChanged 接口。

About ListView group, you can take a look:关于 ListView 组,你可以看看:

https://github.com/xamarin/xamarin-forms-samples/tree/master/UserInterface/ListView/Grouping https://github.com/xamarin/xamarin-forms-samples/tree/master/UserInterface/ListView/Grouping

If you still have another question, I suggest you can create new thread to ask, because this thread is very long.如果你还有其他问题,我建议你可以创建一个新线程来问,因为这个线程很长。

Please remember to mark the helpful reply as answer, thanks.请记住将有用的回复标记为答案,谢谢。

to set a binding programatically以编程方式设置绑定

// set the BindingContext for the page
this.BindingContext = new MyViewModel();

// Title is a public property on MyViewModel
myLabel.SetBinding(Label.TextProperty, "Title");

in order for the UI to update when the VM is changed, the VM needs to implement INotifyPropertyChanged为了在 VM 更改时 UI 更新,VM 需要实现INotifyPropertyChanged

This is some guidance that might help with your problem.这是一些可能有助于解决您的问题的指导。 Your code is messy and I think that is causing your confusion (you have several things named very similarly).你的代码很乱,我认为这会导致你的困惑(你有几个名字非常相似的东西)。

int Nbr_Objet;

public int nbr_objet { get{...} set {...}}

this.Nbr_Objet= Nbr_Objet;

this shows me that you are setting your member variable Nbr_Objet directly, when you do that the property change notification doesn't fire - you need to assign the value through the public nbr_objet for that to happen.这表明您正在直接设置您的成员变量Nbr_Objet ,当您这样做时,属性更改通知不会触发 - 您需要通过公共nbr_objet分配值才能发生这种情况。

I'd suggest you define the binding in XAML, and make sure you bind to the property nbr_objet , not the private member variable (field) Nbr_Objet .我建议您在 XAML 中定义绑定,并确保绑定到属性nbr_objet ,而不是私有成员变量(字段) Nbr_Objet If you want to avoid confusion, follow the C# coding standard and name your member variable _nbrObjet , and camel case your property name public int NbrObjet { get {... .如果您想避免混淆,请遵循 C# 编码标准并将您的成员变量命名为_nbrObjet ,并将您的属性名称驼峰式命名public int NbrObjet { get {...

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

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