C# 当用户单击databound CollectionView项时替换ObservableCollection项的最佳方法
我是Xamarin表单数据绑定和可观察集合的新手 我的问题是,当我替换ObservableCollection中的项目时,是否会收到通知。让我告诉你我的目标。首先,有点问题的背景:C# 当用户单击databound CollectionView项时替换ObservableCollection项的最佳方法,c#,xamarin,data-binding,collections,time-complexity,C#,Xamarin,Data Binding,Collections,Time Complexity,我是Xamarin表单数据绑定和可观察集合的新手 我的问题是,当我替换ObservableCollection中的项目时,是否会收到通知。让我告诉你我的目标。首先,有点问题的背景: ObservableCollection位于viemModel上,例如ObservableCollection 集合绑定到Xamarin CollectionView,显示一些客户信息和更新客户状态的按钮 作为对按钮点击事件的响应,我使用Rest服务并发布一篇文章来更新点击的客户 Post方法返回新项目:修改后的客
- ObservableCollection位于viemModel上,例如ObservableCollection李>
- 集合绑定到Xamarin CollectionView,显示一些客户信息和更新客户状态的按钮
- 作为对按钮点击事件的响应,我使用Rest服务并发布一篇文章来更新点击的客户
- Post方法返回新项目:修改后的客户
var index = viewModel.Items.IndexOf(originalCustomer);
viewModel.Items[index] = updatedCustomer;
但这种方法涉及使用Collection.IndexOf(customer)获取要替换的项的索引,这似乎是一个O(n)操作?有没有办法直接在O(1)复杂度的点击事件上获取点击客户的索引?
提前谢谢
但这种方法涉及使用Collection.IndexOf(customer)获取要替换的项的索引,这似乎是一个O(n)操作。?有没有办法直接在O(1)复杂度的点击事件上获取点击客户的索引
如果您想在按钮单击事件中获取当前索引,我建议您可以绑定Collectionview SelectedItem,然后您可以在更改SelectedItem时获取此索引。请确认已将SelectedMode设置为Single
我为您创建简单,请看一下:
<StackLayout>
<CollectionView
ItemsSource="{Binding customers}"
SelectedItem="{Binding selecteditem}"
SelectionMode="Single">
<CollectionView.ItemTemplate>
<DataTemplate>
<StackLayout Orientation="Horizontal">
<Label Text="{Binding Name}" />
<Label Text="{Binding Age}" />
</StackLayout>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
<Button
x:Name="btn1"
Clicked="Btn1_Clicked"
Text="Get Index" />
</StackLayout>
public partial class Page2 : ContentPage, INotifyPropertyChanged
{
public ObservableCollection<customer> customers { get; set; }
private customer _selecteditem;
public customer selecteditem
{
get { return _selecteditem; }
set
{
_selecteditem = value;
RaisePropertyChanged("selecteditem");
}
}
public Page2()
{
InitializeComponent();
customers = new ObservableCollection<customer>()
{
new customer(){Name="cherry",Age=28},
new customer(){Name="Wendy",Age=28},
new customer(){Name="Jessie",Age=28},
new customer(){Name="Barry",Age=28},
new customer(){Name="Jason",Age=28},
new customer(){Name="Annine",Age=28},
new customer(){Name="Jack",Age=28},
new customer(){Name="Leo",Age=28},
};
this.BindingContext = this;
}
private void Btn1_Clicked(object sender, EventArgs e)
{
var index = customers.IndexOf(selecteditem);
Console.WriteLine("Current index is {0}",index);
}
public event PropertyChangedEventHandler PropertyChanged;
public void RaisePropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
公共分部类Page2:ContentPage,INotifyPropertyChanged
{
公共ObservableCollection客户{get;set;}
私人客户_selecteditem;
公共客户选择项
{
获取{return\u selecteditem;}
设置
{
_选择editem=值;
RaisePropertyChanged(“selecteditem”);
}
}
公共页2()
{
初始化组件();
客户=新的ObservableCollection()
{
新客户(){Name=“cherry”,年龄=28},
新客户(){Name=“Wendy”,年龄=28},
新客户(){Name=“Jessie”,年龄=28},
新客户(){Name=“Barry”,年龄=28},
新客户(){Name=“Jason”,年龄=28},
新客户(){Name=“Annine”,年龄=28},
新客户(){Name=“Jack”,年龄=28},
新客户(){Name=“Leo”,年龄=28},
};
this.BindingContext=this;
}
私有无效Btn1_已单击(对象发送方,事件参数e)
{
var index=customers.IndexOf(selecteditem);
WriteLine(“当前索引为{0}”,索引);
}
公共事件属性更改事件处理程序属性更改;
public void RaisePropertyChanged(字符串propertyName)
{
PropertyChangedEventHandler处理程序=PropertyChanged;
if(处理程序!=null)
{
处理程序(这是新的PropertyChangedEventArgs(propertyName));
}
}
}
但在Button click事件中,我可以通过Button BindingContext获取单击的项目,因此,我不需要绑定SelectedItem属性。不管怎么说,您使用customer.IndexOf(item)来获取项目索引,这是我的问题:?有没有办法不使用IndexOf(以O(1)的方式)来获取已单击项目的索引?@Fran,如果您想直接获取项目索引,请不要使用IndexOf(item),我建议您可以尝试在customer类中添加一个int-type属性作为ID,由于CollectionView选择了索引,所以您可以从项目中获取Id。