Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/261.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# 当用户单击databound CollectionView项时替换ObservableCollection项的最佳方法_C#_Xamarin_Data Binding_Collections_Time Complexity - Fatal编程技术网

C# 当用户单击databound CollectionView项时替换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方法返回新项目:修改后的客

我是Xamarin表单数据绑定和可观察集合的新手

我的问题是,当我替换ObservableCollection中的项目时,是否会收到通知。让我告诉你我的目标。首先,有点问题的背景:

  • ObservableCollection位于viemModel上,例如ObservableCollection
  • 集合绑定到Xamarin CollectionView,显示一些客户信息和更新客户状态的按钮
  • 作为对按钮点击事件的响应,我使用Rest服务并发布一篇文章来更新点击的客户
  • Post方法返回新项目:修改后的客户
此时,我想知道更新集合以便刷新用户界面的最佳方法

我知道我可以更新项目的属性,但要使这种方式起作用,我必须在我的客户(POCO对象)上实现INotifyPropertyChange。这不是问题,但有时候Rest服务更新了很多属性,我更愿意用Post方法返回的新客户替换ObservableCollection中编辑的客户,而不是逐个更新属性

因此,我用以下代码替换集合上的对象,并且工作正常:

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。