Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将ObservableCollection排序为Xamarin.Form中的字母表_C#_Xamarin - Fatal编程技术网

C# 如何将ObservableCollection排序为Xamarin.Form中的字母表

C# 如何将ObservableCollection排序为Xamarin.Form中的字母表,c#,xamarin,C#,Xamarin,我有一个可观察的收集列表,我想把它按字母表排序 例如:当我输入3个名字时(篮球、阿拉伯、俱乐部) 输出: 篮球, 阿拉伯的 俱乐部 我希望它像: 阿拉伯的 篮球, 俱乐部 此代码用于ObservableCollection: public ObservableCollection<Contact> ContactList { get; set; } publicobservableCollection联系人列表{get;set;} 这是添加和删除项目的代码 p

我有一个可观察的收集列表,我想把它按字母表排序 例如:当我输入3个名字时(篮球、阿拉伯、俱乐部) 输出: 篮球, 阿拉伯的 俱乐部

我希望它像: 阿拉伯的 篮球, 俱乐部

此代码用于ObservableCollection:

        public ObservableCollection<Contact> ContactList { get; set; }

publicobservableCollection联系人列表{get;set;}
这是添加和删除项目的代码

 public ContactsListView()
        {

            InitializeComponent();
            ContactList = new ObservableCollection<Contact>();

            BindingContext =  new Contact();
            MessagingCenter.Subscribe<Contact>(this, "addNew", (addItem) =>
            {
                ContactList.Add(new Contact { Name = addItem.Name, PhoneNo = addItem.PhoneNo, Address = addItem.Address, NickName = addItem.NickName });

            });

            MessagingCenter.Subscribe<Contact>(this, "deleteContact", (Account) =>
            {
                ContactList.Remove(Account);
            });

                        MyListView.ItemsSource = ContactList;
            AddNewContact.Clicked+= AddNewContact_Clicked;
        }

公共联系人列表视图()
{
初始化组件();
ContactList=新的ObservableCollection();
BindingContext=新联系人();
MessagingCenter.Subscribe(此“addNew”,(addItem)=>
{
ContactList.Add(新联系人{Name=addItem.Name,PhoneNo=addItem.PhoneNo,addItem.Address,昵称=addItem.昵称});
});
MessagingCenter.Subscribe(此“删除联系人”,(帐户)=>
{
联系人列表。删除(帐户);
});
MyListView.ItemsSource=联系人列表;
AddNewContact.Clicked+=AddNewContact\u Clicked;
}

您可以使用linq进行如下排序:

var sortedContactList = ContactList.ToList().OrderBy(x => x.Name);

您需要使用System.Linq添加
位于文件顶部。

您可以使用linq进行如下排序:

var sortedContactList = ContactList.ToList().OrderBy(x => x.Name);
您需要使用System.Linq添加
位于文件顶部

ObservableCollection类本身没有排序方法。 但通过使用集合,您可以重新创建它

var ContactList=新的可观察集合{“俱乐部”、“阿拉伯”、“篮球”};
ContactList=newobserveCollection(ContactList.OrderBy(i=>i));
ObservableCollection类本身没有排序方法。 但通过使用集合,您可以重新创建它

var ContactList=新的可观察集合{“俱乐部”、“阿拉伯”、“篮球”};
ContactList=newobserveCollection(ContactList.OrderBy(i=>i));
您可以使用
移动
方法对当前的
联系人列表
进行排序,而不是将新排序的
可观察收集
重新创建到
联系人列表

private void SortContacts()
{
    // order by name
    var sortedContact = ContactList.OrderBy(x => x.Name).ToList();

    for (var i = 0; i < sortedContact.Count; i++)
        ContactList.Move(ContactList.IndexOf(sortedContact[i]), i);
}
private void SortContacts()
{
//点名
var sortedContact=ContactList.OrderBy(x=>x.Name.ToList();
对于(变量i=0;i
添加或删除联系人项目后调用上述方法。

您可以使用
移动
方法对当前的
联系人列表
进行排序,而不是将新排序的
可观察集合
重新创建到
联系人列表

private void SortContacts()
{
    // order by name
    var sortedContact = ContactList.OrderBy(x => x.Name).ToList();

    for (var i = 0; i < sortedContact.Count; i++)
        ContactList.Move(ContactList.IndexOf(sortedContact[i]), i);
}
private void SortContacts()
{
//点名
var sortedContact=ContactList.OrderBy(x=>x.Name.ToList();
对于(变量i=0;i

在添加或删除联系人项目后调用上述方法。

可能重复的可能重复的可能重复的我对此不是非常清楚,但我想我已经看到它提到在ObservableCollection上使用linq会破坏绑定。是的,除非有完整的集合,否则无法排序。我对此不是非常清楚,但我想我已经看到它提到在ObservableCollection上使用linq会破坏绑定。是的,除非有完整的集合,否则无法排序。