【问题标题】:change the order in ListView更改 ListView 中的顺序
【发布时间】:2020-07-31 07:27:49
【问题描述】:

这是我的第一个 xamarin 格式的应用程序。我需要修改列表顺序,但找不到方法。我需要按AutoModelo 的顺序显示它。我将源代码发送给您,以防有人可以帮助我。

这是模型/Auto.cs

 {
        public class Auto
        {
            public Guid AutoID { get; set; }
            public string AutoPatente { get; set; }
            public string AutoModelo { get; set; }
            public string AutoColor { get; set; }
            public bool AutoLuces { get; set; }
            public int AutoKmtablero { get; set; }
            public List<Actividad> Actividades { get; set; }
        }
    }

这是 ViewModels/AutoListPageViewModel

namespace CracApp.ViewModels
{
    public class AutoListPageViewModel : BaseViewModel
    {

        string controller = Constants.AutosApi;
        public INavigation Navigation { get; set; }
        public ICommand NewAutoCommand { get; set; }
        public ICommand ViewAutoCommand { get; set; }


        Auto selectedAuto;

        public Auto SelectedAuto
        {
            get => selectedAuto;
            set { selectedAuto = value; RaisePropertyChanged(); }
        }

        private ObservableCollection<Auto> autosList;       

        //Collection = new ObservableCollection(Collection.OrderBy(x=>x.Date).ToList());


        public ObservableCollection<Auto> AutosList
        {

            get  => autosList;
            set { 
                autosList = value; 
                RaisePropertyChanged();                               
            }

        }

        public AutoListPageViewModel(INavigation navigation)
        {
            Navigation = navigation;
            NewAutoCommand = new Command(async () => await NewAuto());
            ViewAutoCommand = new Command(async () => await ViewAuto());
        }

        public async Task LoadData()
        {
            IsBusy = true;
            AutosList = new ObservableCollection<Auto>(await AutoService<Auto>.GetData(controller));
            //Collection = new ObservableCollection(Collection.OrderBy(x=>x.Date).ToList());
            IsBusy = false;
        }

        async Task NewAuto()
        {

            //await Navigation.PushAsync(new Views.AutoDetailPage   (new AutoDetailViewModel       (new Auto(),      Navigation)));
            //await Navigation.PushAsync(new Views.ActividadListPage(new ActividadListPageViewModel(new Actividad(), Navigation)));
             //await Navigation.PushAsync(new ActividadListPage(new ActividadListPageViewModel(new Actividad(), Navigation)));
            await Navigation.PushAsync(new ActividadListPage());
        }

        async Task ViewAuto()
        {
            if (SelectedAuto != null)
            {
                await Navigation.PushAsync(new Views.AutoDetailPage(new AutoDetailViewModel(SelectedAuto, Navigation)));

            } 
        }

    }
}

【问题讨论】:

  • 您能否标记/接受对您有帮助的答案,以便我们可以帮助更多有同样问题的人:)。
  • 是的,当然。我应该在页面的哪个位置进行操作?
  • 您的页面中应该有一个“接受”选项。
  • 你能标记答案吗?

标签: xamarin.forms


【解决方案1】:

使用 LINQ OrderBy

var sorted = autosList.OrderBy(a => a.AutoModelo);

在你的上下文中,可能是这样的

var data = wait AutoService<Auto>.GetData(controller);
AutosList = new ObservableCollection<Auto>(data.OrderBy(a => a.AutoModelo).ToList());

【讨论】:

  • 在这里?公共 ObservableCollection AutosList { get => autosList;设置 { autosList = 值; var sorted = autosList.OrderBy(a => a.AutoModelo);提高属性更改(); }
  • 嗯,没有。这没有任何意义。查看我的编辑
猜你喜欢
  • 2011-06-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-31
  • 1970-01-01
  • 2013-09-22
  • 2012-04-18
相关资源
最近更新 更多