Xamarin 数据绑定 - 通过 ItemList 显示来自自定义 class 的数据的问题
Xamarin Data Binding - issue showing data from custom class via ItemList
我在模型中定义了以下 class:
public class Equipment
{
public Equipment(string resName, DateTime crTime, int quant)
{
ResourceName = resName;
CreateTime = crTime;
Quantity = quant;
}
public string ResourceName { get; set; }
public DateTime CreateTime { get; set; }
public int Quantity { get; set; }
}
我使用 viewModel,我将其定义为
class EquipementViewModel : ObservableObject
{
ObservableCollection<Equipment> equipRange = new ObservableCollection<Equipment>();
public ObservableCollection<Equipment> EquipRange { get { return equipRange; } }
public EquipementViewModel()
{
equipRange.Add(new Equipment("Komax01",DateTime.Now,1));
equipRange.Add(new Equipment("Komax02", DateTime.Now, 1));
equipRange.Add(new Equipment("Komax03", DateTime.Now, 1));
}
}
最后是视图 XAML
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:viewmodels="clr-namespace:TestXamarin.ViewModels"
x:Class="TestXamarin.Views.EquipementPage"
Title="EQuip"
x:DataType="viewmodels:EquipementViewModel">
<ContentPage.BindingContext>
<viewmodels:EquipementViewModel/>
</ContentPage.BindingContext>
<ListView ItemsSource="{Binding EquipRange}" BackgroundColor="Transparent">
<ListView.ItemTemplate>
<DataTemplate>
<TextCell TextColor="black" Text="{Binding ResourceName}">
<TextCell.BindingContext>
<viewmodels:EquipementViewModel/>
</TextCell.BindingContext></TextCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</ContentPage>
我想让 TextCell 显示设备的 ResourceNasme class,但是 VIsual Studio 向我显示了一个错误,即在数据上下文“设备视图模型”中找不到成员。
我看了很多指南,我不认为我做错了什么。可能是什么问题?
您不需要为 TextCell
提供 BindingContext
。它会自动从 ListView
继承正确的 BindingContext
<ListView.ItemTemplate>
<DataTemplate>
<TextCell TextColor="black" Text="{Binding ResourceName}">
<TextCell.BindingContext>
<viewmodels:EquipementViewModel/>
</TextCell.BindingContext></TextCell>
</DataTemplate>
</ListView.ItemTemplate>
而只是这样做
<ListView.ItemTemplate>
<DataTemplate>
<TextCell TextColor="black" Text="{Binding ResourceName}" />
</DataTemplate>
</ListView.ItemTemplate>
我在模型中定义了以下 class:
public class Equipment
{
public Equipment(string resName, DateTime crTime, int quant)
{
ResourceName = resName;
CreateTime = crTime;
Quantity = quant;
}
public string ResourceName { get; set; }
public DateTime CreateTime { get; set; }
public int Quantity { get; set; }
}
我使用 viewModel,我将其定义为
class EquipementViewModel : ObservableObject
{
ObservableCollection<Equipment> equipRange = new ObservableCollection<Equipment>();
public ObservableCollection<Equipment> EquipRange { get { return equipRange; } }
public EquipementViewModel()
{
equipRange.Add(new Equipment("Komax01",DateTime.Now,1));
equipRange.Add(new Equipment("Komax02", DateTime.Now, 1));
equipRange.Add(new Equipment("Komax03", DateTime.Now, 1));
}
}
最后是视图 XAML
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:viewmodels="clr-namespace:TestXamarin.ViewModels"
x:Class="TestXamarin.Views.EquipementPage"
Title="EQuip"
x:DataType="viewmodels:EquipementViewModel">
<ContentPage.BindingContext>
<viewmodels:EquipementViewModel/>
</ContentPage.BindingContext>
<ListView ItemsSource="{Binding EquipRange}" BackgroundColor="Transparent">
<ListView.ItemTemplate>
<DataTemplate>
<TextCell TextColor="black" Text="{Binding ResourceName}">
<TextCell.BindingContext>
<viewmodels:EquipementViewModel/>
</TextCell.BindingContext></TextCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</ContentPage>
我想让 TextCell 显示设备的 ResourceNasme class,但是 VIsual Studio 向我显示了一个错误,即在数据上下文“设备视图模型”中找不到成员。
我看了很多指南,我不认为我做错了什么。可能是什么问题?
您不需要为 TextCell
提供 BindingContext
。它会自动从 ListView
继承正确的 BindingContext
<ListView.ItemTemplate>
<DataTemplate>
<TextCell TextColor="black" Text="{Binding ResourceName}">
<TextCell.BindingContext>
<viewmodels:EquipementViewModel/>
</TextCell.BindingContext></TextCell>
</DataTemplate>
</ListView.ItemTemplate>
而只是这样做
<ListView.ItemTemplate>
<DataTemplate>
<TextCell TextColor="black" Text="{Binding ResourceName}" />
</DataTemplate>
</ListView.ItemTemplate>