2017-05-11 46 views
0

我是Xamarin的新手,我試圖將ViewModel綁定到視圖,但我還沒有做到。Xamarin窗體綁定不顯示在ListView上

這是代碼。

(型號)

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

(可觀察模型)

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)); 
      } 
     } 
    } 
} 

(視圖模型)

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; 
      } 
     } 
    } 
} 

(查看)

<?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> 

它沒有奏效,它沒有在列表中顯示這些元素。有人能幫我嗎?

在此先感謝。

+0

你的ObservableProduto是錯誤的,只包含1個Produto(而不是你的構造函數中寫的3)...並且你不需要爲你的ObservableCollection獲取/設置,因爲它已經在你的構造函數中創建了......但無論如何它不會解決你的問題... – hugoterelle

回答

1

你不太瞭解MVVM的方法,但你幾乎在那裏。您不需要擁有ObservableProduto類。你可以讓你的模型成爲你的Produto類。

這是你的Produto模型。我繼續爲你改變它。

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)); 
    } 
} 

你應該包含您的Produto S的ObservableCollection在視圖模型。我看到你已經做到了。我已經編輯了一下。您可能需要小心在一臺機器上完全重置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; 
      } 
     } 
    } 
} 

注意:您需要將項目添加到您的ObservableColleciton仍然。

+0

謝謝先生!在這些更改後出現錯誤「類型爲'Xamarin.Forms.TextCell'的對象無法轉換爲類型'Xamarin.Forms.View'」 – freitas

+0

這是因爲您在'StackLayout'內有一個'TextCell'。刪除'StackLayout'。你只需要'TextCell'。 「ListView」顯示的所有內容都需要某種形式的Cell或從ViewCell繼承,而StackLayout顯示的所有內容都需要某種形式的View。 –