下面是一些示例,我编写了我的第一个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个元素),但标签内没有任何东西。
谢谢配偶。
“属性”实际上是“Intervenciones”类型的公共属性吗? –
请提供班级Intervenciones。 – Dominik
嗨@StefanWanitzek。是的。 –