在每个论坛上在线搜索后,我决定发表自己的问题:我的listview数据在IOS中正确显示,但在Android中未正确显示。我绑定到我的视图模型中的属性,并且看起来正确,但是我无法弄清缺少的内容(我正在尝试获取要填充列表视图的订单列表)。任何帮助表示赞赏!
此外,我使用的是选项卡式页面布局。不确定是否有所不同。
//View model
public ObservableCollection<GroupedOrderModel> ListOfOrdersGrouped
{
get { return listOfOrdersGrouped; }
set {
listOfOrdersGrouped = value;
NotifyPropertyChanged("listOfOrdersGrouped");
}
}
public class OrderList : List<OrderStatusInfo>
{
public string Heading { get; set; }
public List<OrderStatusInfo> Orders
{
get { return this; }
set { Orders = value; }
}
}
public async Task<bool> BuildListviewData()
{
await Task.Run(()=> {
ObservableCollection<GroupedOrderModel> orderList = new ObservableCollection<GroupedOrderModel>();
if (GlobalInfo.GlobalOrderList != null && GlobalInfo.GlobalOrderList.Count > 0)
{
List<string> Headers = GlobalInfo.GlobalOrderList.Select(x => x.Status).Distinct().OrderBy(x => x).ToList();
foreach (var item in Headers)
{
var oGroup = new GroupedOrderModel
{
Heading = item
};
var oList = GlobalInfo.GlobalOrderList.Where(x => x.Status == item).ToList();
foreach (var o in oList)
oGroup.Add(o);
orderList.Add(oGroup);
}
ListOfOrdersGrouped = orderList;
}
}
}
//Orders.cs page
protected async override void OnAppearing()
{
await ovm.BuildListviewData();
BindingContext = ovm;
base.OnAppearing();
}
//The XAML
<ListView x:Name="ListViewOrdersList" HasUnevenRows="True" ItemsSource="{Binding ListOfOrdersGrouped}" IsGroupingEnabled="True">
<ListView.GroupHeaderTemplate>
<DataTemplate>
<ViewCell>
<Label Text="{Binding Heading}" Margin="10"/>
</ViewCell>
</DataTemplate>
</ListView.GroupHeaderTemplate>
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Grid Padding="10">
<Label Text="{Binding OrderNo}" FontSize="20" HorizontalOptions="Start"/>
</Grid>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
答案 0 :(得分:0)
将ItemSource属性的更改为
private listOfOrdersGrouped;
public ObservableCollection ListOfOrdersGrouped
{
get { return listOfOrdersGrouped; }
set {
listOfOrdersGrouped = value;
NotifyPropertyChanged("ListOfOrdersGrouped");
}
}
NotifyPropertyChanged应该为:
NotifyPropertyChanged("ListOfOrdersGrouped");
以便该名称与您在XAML中绑定到的属性的名称匹配。
答案 1 :(得分:0)
对于遇到相同问题的任何人:
问题最终成为我的视图模型是页面特定的问题(例如,Orders.xaml.cs页面的OrdersViewModel.cs)。在为App.xaml.cs页面创建AppViewModel.cs并将我的OrdersViewModel属性转储到名为OrdersViewModel的新类之后,由于项目在启动前运行了App.xaml和App.xaml.cs页面,因此所有工作正常还有其他事情。这就是为什么绑定在IOS而不是Android中起作用的原因。在绑定完成实例化之前,Android试图处理每个选项卡式页面的所有代码(因此,没有数据!)。
但是,我做了一些微调。这是更改:
namespace MyApp.ViewModels
{
public class AppViewModel
{
public OrdersViewModel Ovm { get; set; }
}
public class OrdersViewModel : INotifyPropertyChanged
{
public ObservableCollection<GroupedOrderModel> ListOfOrdersGrouped
{
get { return listOfOrdersGrouped; }
set
{
listOfOrdersGrouped = value;
NotifyPropertyChanged("ListOfOrdersGrouped");
}
}
public async Task<bool> BuildListviewData()
{
//Same code here as mentioned above
}
}
//Deleted OrderList class mentioned above
public class GroupedOrderModel : ObservableCollection<OrderStatusInfo>
{
public string Heading { get; set; }
}
}
//Xaml
<ListView x:Name="ListViewOrdersList" HasUnevenRows="True" ItemsSource="{Binding Ovm.ListOfOrdersGrouped}" IsGroupingEnabled="True">
<ListView.GroupHeaderTemplate>
<DataTemplate>
<ViewCell>
<Label Text="{Binding Heading}" Margin="10"/>
</ViewCell>
</DataTemplate>
</ListView.GroupHeaderTemplate>
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Grid Padding="10">
<Label Text="{Binding OrderNo}" FontSize="20" HorizontalOptions="Start"/>
</Grid>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
编码愉快!