2016-09-26 47 views
0

下面是一些示例,我编写了我的第一个MVVM的演示。绑定和渲染基本的后,我试图将一个列表绑定到一个列表视图,我做错了什么,但我无法得到它。MVVM示例不工作

如果有人能给我任何线索,我将不胜感激。

的想法是这是一个:

- 视图模型 -

public class IntervectionViewModel : INotifyPropertyChanged 
    { 
     public event PropertyChangedEventHandler PropertyChanged; 

     public void RaiseOnPropertyChange([CallerMemberName] string propertyName = null) 
     { 
      if (PropertyChanged != null) 
      { 
       PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
      } 
     } 

     public IntervectionViewModel() 
     { 
      intervencion = new IntervectionModel(); 
      Intervencion1 = intervencion.Intervencion; 
     } 

     private List<Intervenciones> _intervencion1; 
     public List<Intervenciones> Intervencion1 
     { 
      get { return _intervencion1; } 
      set { _intervencion1 = value; RaiseOnPropertyChange(); } 
     } 
    } 

- 模型 -

public class IntervectionModel 
    { 
     public List<Intervenciones> Intervencion; 
     public IntervectionModel() 
     { 
      for (int i = 0; i < 4; i++) 
      { 
//Class Intervenciones contains attribute 
       Intervenciones inter = new Intervenciones(i); 
       this.Intervencion.Add(inter); 
      } 
     } 
    } 

    public class Intervenciones 
    { 
     public string Nombre; 

     public Intervenciones(int num) 
     { 
      this.Jefe = "JEFE" + num + " = JEFE1"; 
      this.Nombre = "NOMBRE" + num + " = NOMBRE1"; 
      this.Timer = "1:23:23"; 
      this.Estado = Enums.Estado.Bloqueado; 
      this.Interv = new List<Tarea>(); 

      for (int i = 0; i < 8; i++) 
      { 
       Tarea t = new Tarea 
       { 
        Titulo = this.Nombre + "%" + "TITULO = " + i, 
        Inicio = DateTime.Now.AddMinutes(i), 
        Duracion = i * 60, 
        Encargado = "ENCARGADO = " + i, 
        Estado = Enums.Estado.Activo, 
        Miembros = new List<string> { "Miembro" + num + " = " + i, "Miembro1" + num + " = " + i, "Miembro2" + num + " = " + i }, 
        TiempoEjecucion = i + 15 
       }; 
       Interv.Add(t); 
      } 
     } 

    } 
} 

- XAML.CS -

public partial class Page1 : ContentPage 
{ 
    public Page1() 
    { 
     var p = new IntervectionViewModel(); 
     BindingContext = p; 
     InitializeComponent(); 
    } 
} 

- XAML -

<ListView x:Name="sa" ItemsSource="{ Binding Intervencion1 }" VerticalOptions="Center" HorizontalOptions="Center"> 
    <ListView.ItemTemplate> 
     <DataTemplate> 
     <ViewCell> 
      <Label Text="{ Binding attribute }" TextColor="Black" BackgroundColor="Blue"/> 
      </ViewCell> 
     </DataTemplate> 
    </ListView.ItemTemplate> 
    </ListView> 

XAML正在绘制4个蓝色的行(列表绑定了4个元素),但标签内没有任何东西。

谢谢配偶。

+1

“属性”实际上是“Intervenciones”类型的公共属性吗? –

+0

请提供班级Intervenciones。 – Dominik

+0

嗨@StefanWanitzek。是的。 –

回答

2

类Intervenciones需要一个字符串属性 “属性” 像

public string attribute { get; set; } 

您的信息:

它必须是一个属性!不是一个领域。而实际上约定是:

public string Attribute { get; set; } 

因为它是一个公共财产。

+0

感谢Dominik。在斯蒂芬和你之间,我明白了。我可以实现一个真正的。干杯伴侣。 –

0

除此之外,你不应该在你的视图后面的代码中实例化你的ViewModel。

尝试将您的ViewModel设置为View的DataContext而不是BindingContext。而且你似乎没有在模型中实现属性“属性”。