简体   繁体   English

Xamarin表单绑定未显示在ListView上

[英]Xamarin Forms Binding not showing on ListView

I'm new to Xamarin and I'm trying to bind my ViewModel to the View but I couldn't do it yet. 我是Xamarin的新手,正在尝试将ViewModel绑定到View,但目前还无法完成。

Here's the code. 这是代码。

(Model) (模型)

namespace CadastroProdutos
{
    public class Produto
    {
        public string Codigo { get; set; }
        public string Identificacao { get; set; }
        public string Tipo { get; set; }
    }
}

(Observable Model) (可观察模型)

namespace CadastroProdutos
{
    public class ObservableProduto : INotifyPropertyChanged
    {
        Produto produto;
        public ObservableProduto()
        {
            produto = new Produto()
            {
                Identificacao = "Primeiro",
                Codigo = "123456"

            };
            produto = new Produto()
            {
                Identificacao = "Segundo",
                Codigo = "123456"

            };
            produto = new Produto()
            {
                Identificacao = "Terceiro",
                Codigo = "123456"

            };
        }

        public event PropertyChangedEventHandler PropertyChanged;

        public string Codigo
        {               
            set
            {
                if (!value.Equals(produto.Codigo, StringComparison.Ordinal))
                {
                    produto.Codigo = value;
                    OnPropertyChanged("Codigo");
                }
            }
            get
            {
                return produto.Codigo;
            }
        }

        public string Identificacao
        {
            set
            {
                if (!value.Equals(produto.Identificacao, StringComparison.Ordinal)) 
                {
                    produto.Identificacao = value;
                    OnPropertyChanged("Identificacao");
                }
            }
            get
            {
                return produto.Identificacao;
            }
        }

        public string Tipo
        {
            set
            {
                if (!value.Equals(produto.Tipo, StringComparison.Ordinal)) 
                {
                    produto.Tipo = value;
                    OnPropertyChanged("Tipo");
                }
            }
            get
            {
                return produto.Tipo;
            }
        }

        void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            var handler = PropertyChanged;
            if (handler == null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}

(ViewModel) (视图模型)

namespace CadastroProdutos
{
    public class ListProdutoViewModel
    {
        ObservableCollection<ObservableProduto> produtos;

        public ListProdutoViewModel()
        {
            produtos = new ObservableCollection<ObservableProduto>();
        }

        public ObservableCollection<ObservableProduto> Produtos
        {
            set
            {
                if (value != produtos)
                {
                    produtos = value;
                }
            }
            get
            {
                return produtos;
            }
        }
    }
}

(View) (视图)

<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" 
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
    xmlns:local="clr-namespace:CadastroProdutos;"
    x:Class="CadastroProdutos.ListProduto"
    Title="Listagem de Produtos">
    <ContentPage.Content>
        <ListView x:Name="listView" Margin="20,40,20,20" ItemsSource="{Binding Produtos}">
            <ListView.BindingContext>
                <local:ListProdutoViewModel />
            </ListView.BindingContext>
            <ListView.Header>
                <StackLayout Orientation="Vertical" >
                        <Label Text="Produtos" HorizontalOptions="Center"/>
                </StackLayout>
            </ListView.Header>
            <ListView.ItemTemplate>
                <DataTemplate>
                    <StackLayout Orientation="Horizontal" >
                        <TextCell Text="{Binding Identificacao}"/>
                    </StackLayout>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </ContentPage.Content>
</ContentPage>

It not worked, It didn't show those elements on the list. 它不起作用,它没有在列表中显示那些元素。 Can someone help me? 有人能帮我吗?

Thanks in advance. 提前致谢。

You're not quite understanding the MVVM approach, but you're almost there. 您不太了解MVVM方法,但是您几乎可以理解了。 You don't need to have the ObservableProduto class. 您不需要具有ObservableProduto类。 You can make your Produto class your model. 您可以使Produto类成为您的模型。

This is your Produto model. 这是您的Produto模型。 I went ahead and changed it up for you. 我继续为您进行了更改。

namespace CadastroProdutos
{
    public class Produto : INotifyPropertyChanged
    {
        private string codigo;
        public string Codigo 
        { 
            get {return codigo;} 
            set {codigo=value; OnPropertyChanged(); }
        }

        private string identificacao;
        public string Identificacao 
        { 
            get {return identificacao;} 
            set {identificacao=value; OnPropertyChanged(); }
        }

        private string tipo ;
        public string Tipo 
        { 
            get {return tipo;} 
            set {tipo=value; OnPropertyChanged(); }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        void OnPropertyChanged([CallerMemberName]string propertyName = "") =>
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

You should contain an ObservableCollection of your Produto s in a viewmodel. 你应该包含ObservableCollectionProduto在一个视图模型秒。 I see you've done that. 我知道你已经做到了。 I've edited it a bit. 我已经编辑了一下。 You may need to be careful about totally resetting your ObservableCollection on a set. 您可能需要谨慎地完全重置某个集合上的ObservableCollection

namespace CadastroProdutos
{
    public class ListProdutoViewModel
    {
        ObservableCollection<Produto> produtos;

        public ListProdutoViewModel()
        {
            produtos = new ObservableCollection<Produto>();
        }

        public ObservableCollection<Produto> Produtos
        {
            set
            {
                if (value != produtos)
                {
                    produtos = value;
                }
            }
            get
            {
                return produtos;
            }
        }
    }
}

Note: you will need to add items to your ObservableColleciton still. 注意:您仍然需要将项目添加到ObservableColleciton

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

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