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
单击选取器中的项目时,从listview xamarin中删除项目_Listview_Xamarin_Xamarin.forms_Picker_Restapi - Fatal编程技术网

单击选取器中的项目时,从listview xamarin中删除项目

单击选取器中的项目时,从listview xamarin中删除项目,listview,xamarin,xamarin.forms,picker,restapi,Listview,Xamarin,Xamarin.forms,Picker,Restapi,在我的列表视图中,我有一列包含一个选择器,我在其中单击“删除”。当我单击该列时,该元素已在数据库中删除,但我无法在“操作已取消”的同时从列表中删除该项。 顺便说一下,我正在使用http服务RESTAPI来删除 xaml 在viewmodel中: public class ResponseDataN { public RootModel Data; } public class RootModel : INotifyPropertyChanged

在我的列表视图中,我有一列包含一个选择器,我在其中单击“删除”。当我单击该列时,该元素已在数据库中删除,但我无法在“操作已取消”的同时从列表中删除该项。 顺便说一下,我正在使用http服务RESTAPI来删除

xaml

在viewmodel中:

    public class ResponseDataN
    {
        public RootModel Data;
    }
    public class RootModel : INotifyPropertyChanged
    {
        public string CompanyName { get; set; }
        public string Name { get; set; }
        public int? Id { get; set; }

        public List<listraps> listRapts { get; set; }
        public RootModel()
        {
            listRapts = new List<listraps> { new listraps() };

                   }
        public Command<listraps> RemoveCommand
        {
            get
            {
                return new Command<listraps>((raport) =>
                {
                    listRapts.Remove(raport);
                });
            }
        }

我看到两件事,1)您已经设置了
ItemsSource=“{Binding Rapports}”
,但是您正在执行一个
listRapts.Remove(raport),因此您希望从融洽关系中删除2)listView应使用ObservableCollection而不是列表。这里有一个很好的例子@Saamer,关于ObservableCollection,我不能这样做,因为当我使用它时,我应该像这样定义变量:employees.Add(newemployee{DisplayName=“Rob Finnerty”});但对我来说,我不能定义DisplayName这样的东西,因为它完全来自于消费服务。你必须明白,当绑定属性改变它的值时,ListView几乎会自动刷新列表。因此,在您的情况下,您必须使用ItemsSource=“{Binding listRapts}”绑定,或者执行一个relations.Remove(raport);你对ObservableCollection的评论毫无意义。您应该能够在不更改模型的情况下使用ObservableCollection代替列表。我看到两件事,1)您已经设置了
ItemsSource=“{Binding relations}”
,但是您正在执行
listRapts.Remove(raport),因此您希望从融洽关系中删除2)listView应使用ObservableCollection而不是列表。这里有一个很好的例子@Saamer,关于ObservableCollection,我不能这样做,因为当我使用它时,我应该像这样定义变量:employees.Add(newemployee{DisplayName=“Rob Finnerty”});但对我来说,我不能定义DisplayName这样的东西,因为它完全来自于消费服务。你必须明白,当绑定属性改变它的值时,ListView几乎会自动刷新列表。因此,在您的情况下,您必须使用ItemsSource=“{Binding listRapts}”绑定,或者执行一个relations.Remove(raport);你对ObservableCollection的评论毫无意义。您应该能够在不更改模型的情况下使用ObservableCollection代替列表。
  private void rapp_SelectedIndexChanged(object sender, EventArgs e)
    {
        var ListR = sender as Picker;
        var item = (listraps)ListR.BindingContext;
        var id = item.Id;
        var choix = ListR.Items[ListR.SelectedIndex];
        switch (choix)
        {
            case "Supprimer":
                {
                      var vm = BindingContext as RootModel;
                    var r = ListR.BindingContext as listraps;
                    vm.RemoveCommand.Execute(r);

                    deleteExpenserapport(id); }
                break;
            case "Modifier":
                DisplayAlert("ok", choix, "ok");
                break;
        }


 public async void deleteExpenserapport(int id)
    {
        listraps r = new listraps();
        r.Id = id;

        var url = "http://192.168.1.3:3000/api/adepApi/DeleteExpenseReport?expenseReportId=" + id;
        var uri = new Uri(url);
        HttpClient client = new HttpClient();
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
        HttpResponseMessage response;
        var json = JsonConvert.SerializeObject(r);
        var content = new StringContent(json, Encoding.UTF8, "application/json");
        response = await client.PostAsync(uri, content);


        if (response.StatusCode== System.Net.HttpStatusCode.Accepted) { 

            await DisplayAlert("Successfully", "you data have been Deleted", "OK"); }
        else
        {
            await DisplayAlert("problem ", "no delete ", "OK");
        }
    }
    public class ResponseDataN
    {
        public RootModel Data;
    }
    public class RootModel : INotifyPropertyChanged
    {
        public string CompanyName { get; set; }
        public string Name { get; set; }
        public int? Id { get; set; }

        public List<listraps> listRapts { get; set; }
        public RootModel()
        {
            listRapts = new List<listraps> { new listraps() };

                   }
        public Command<listraps> RemoveCommand
        {
            get
            {
                return new Command<listraps>((raport) =>
                {
                    listRapts.Remove(raport);
                });
            }
        }
 public class listraps
{
    //public string Titre { get; set; }
    public bool IsVisible { get; set; }

    public int Id { get; set; }
    public  int? UserId { get; set; }
    public string Name { get; set; }
    public string Place { get; set; }
    public DateTime? StartDate { get; set; }
    public DateTime? EndDate { get; set; }
    public int? Duration { get; set; }
    public string Description { get; set; }
    public decimal? Amount { get; set; }
    public decimal? AmountReimbursed { get; set; }}