简体   繁体   English

Xamarin.Forms listview没有在Android中更新

[英]Xamarin.Forms listview not updating in Android

After searching every forum online I decided to post my own question: My listview data is shown correctly in IOS but not in Android. 在网上搜索每个论坛后,我决定发布我自己的问题:我的列表视图数据在IOS中正确显示,但在Android中没有。 I'm binding to a property in my view model and that looks correct but I can't figure out what I'm missing (I'm trying to get a list of orders to populate my listview). 我绑定到我的视图模型中的属性,看起来正确,但我无法弄清楚我缺少的是什么(我正在尝试获取一个订单列表来填充我的listview)。 Any help is appreciated! 任何帮助表示赞赏!

Also, I'm using a tabbed page layout. 另外,我正在使用选项卡式页面布局。 Not sure if that makes a difference or not. 不确定这是否有所作为。

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

Change your ItemSource property's to 将ItemSource属性更改为

private listOfOrdersGrouped;
public ObservableCollection ListOfOrdersGrouped
{

    get { return listOfOrdersGrouped; }
    set {
        listOfOrdersGrouped = value;
        NotifyPropertyChanged("ListOfOrdersGrouped");
    }
}

The NotifyPropertyChanged should be: NotifyPropertyChanged应该是:

NotifyPropertyChanged("ListOfOrdersGrouped");

So that the name matches that of the property your binding to in the XAML. 这样名称就与您在XAML中绑定的属性的名称相匹配。

For anyone struggling with the same problem: 对于遇到同样问题的人:

The problem ended up being an issue with my view model being page specific (Such as OrdersViewModel.cs for my Orders.xaml.cs page). 问题最终成为我的视图模型特定于页面的问题(例如Orders.xaml.cs页面的OrdersViewModel.cs)。 After I created an AppViewModel.cs for my App.xaml.cs page and dumped my OrdersViewModel properties in there under a new class named OrdersViewModel it all worked since the project runs the App.xaml and App.xaml.cs pages on startup before it does anything else. 在我为App.xaml.cs页面创建了一个AppViewModel.cs并将一个名为OrdersViewModel的新类转储到其中的OrdersViewModel属性之后,它在项目运行App.xaml和App.xaml.cs页面之前就已经完成了。做其他事情。 That's why binding was working in IOS and not Android. 这就是为什么绑定在IOS而不是Android中工作的原因。 Android was attempting to process all code for every tabbed page before the binding could finish being instantiated (therefore, no data shown!). Android在绑定完成实例化之前尝试处理每个选项卡页面的所有代码(因此,没有显示数据!)。

However, I did tweak my code a little. 但是,我确实稍微调整了我的代码。 Here are the changes: 以下是更改:

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>

Happy coding! 快乐的编码!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM