简体   繁体   English

可观察到的集合仅删除项目一次,再次尝试时不起作用-Windows Phone 8-C#

[英]Observable collection removing item only once, when tried again doesnt work - Windows phone 8 - C#

I am writing my first application for WP8 platform in C#. 我正在用C#编写WP8平台的第一个应用程序。 I implemented three datatypes namely locationModel which has locationGroups. 我实现了三种数据类型,即具有locationGroups的locationModel。 Each locationGroup has a ObservableCollection of type locationData. 每个locationGroup都有一个locationData类型的ObservableCollection。

locationData has two double types for latitude and longitude and a title string. locationData有两种用于纬度和经度的双重类型和一个标题字符串。

I used a textblock inside a stackpanel to show the locationData element's title, where the lat long are hidden to user. 我在堆栈面板中使用了一个文本块来显示locationData元素的标题,该位置的纬度对用户隐藏了。 There is a context menu on each of this textblock element which enable the user to delete the respective locationData. 在每个textblock元素上都有一个上下文菜单,使用户可以删除相应的locationData。 When I open the app and delete any item, it succesfully does and updates the view too. 当我打开应用程序并删除任何项目时,它也会成功执行并更新视图。 But when I do it for another item it just doesnt work. 但是,当我为另一个项目执行此操作时,它将无法正常工作。 I cannot delete more than one items for each time I open the app. 每次打开该应用程序时,我不能删除多个项目。

I used breakpoints to see where the problem is. 我使用断点来查看问题出在哪里。 the selected locationData is succesfully passed to the App.ViewModel.LocationModel.Items.Remove(). 所选的locationData已成功传递到App.ViewModel.LocationModel.Items.Remove()。 But it just that they are not deleted from the observable collection. 但这只是它们没有从可观察的集合中删除。 I even tried to see the index of the locationData in observable collection and use RemoveAt method. 我什至试图在可观察的集合中查看locationData的索引,并使用RemoveAt方法。 Even it doesnt work. 即使它不起作用。 I did a lot of research to find the problem, but no one seems to face the same problem as me. 我做了很多研究以找到问题所在,但似乎没有人遇到与我相同的问题。 I referred to msdn article on how to implement inotifypropertychanged to update the collection. 我提到了msdn有关如何实现inotifypropertychanged来更新集合的文章。 But its too complex for me to understand that. 但这对我来说太复杂了。

I dont really understand why the observable collection delete the item for the second time even though if I pass the index of that item. 我真的不明白,即使我传递了该项目的索引,为什么可观察的集合还是第二次删除了该项目。 And my usage of breakpoints showed me that the data is not even null. 我对断点的使用向我显示了数据甚至都不为空。

So kindly tell me what is causing this problem and how do I overcome it so that I can implement my own workaround and not face this issue again. 因此,请告诉我是什么导致了此问题,以及如何解决该问题,以便我可以实施自己的解决方法,而不会再次遇到此问题。 I can show you the code if you want me to. 如果您愿意,我可以向您显示代码。 Thanks. 谢谢。

CODE: Adding an item 代码:添加项目

private void SaveLocationData(LocationData locationData)
    {
        IsolatedStorageSettings appSettings = IsolatedStorageSettings.ApplicationSettings;

        try
        {
            App.ViewModel.Custom.Items.Add(locationData);
            var data = JsonConvert.SerializeObject(App.ViewModel.Custom);

            appSettings[LocationModel.CustomKey] = data;
            appSettings.Save();

            //Notify that data is changed
            App.ViewModel.LoadData();
            NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.RelativeOrAbsolute));
        }
        catch(IsolatedStorageException ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

Deleting item: 删除项目:

private void DeleteLocationData(LocationData locationData)
    {
        IsolatedStorageSettings appSettings = IsolatedStorageSettings.ApplicationSettings;

        try
        {
            App.ViewModel.Custom.Items.Remove(locationData);
            var data = JsonConvert.SerializeObject(App.ViewModel.Custom);

            appSettings[LocationModel.CustomKey] = data;
            appSettings.Save();

            //Notify that data is changed
            App.ViewModel.LoadData();
            NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.RelativeOrAbsolute));
        }

        catch (IsolatedStorageException ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

One more thing that I want to say is, whenever I add a locationData to the collection, it updates automatically. 我想说的一件事是,每当我向集合添加locationData时,它都会自动更新。 Because adding is done on another page and when the mainpage.xaml loads(in which the observable collection data is), it updates automatically because of the code in OnNavigatedTo method. 由于添加是在另一页上完成的,并且在mainpage.xaml加载时(可观察的集合数据所在的位置),由于OnNavigatedTo方法中的代码,它会自动更新。

protected override void OnNavigatedTo(NavigationEventArgs e)
    {

        if (!App.ViewModel.IsDataLoaded)
        {
            App.ViewModel.LoadData();
        }

    }

And LoadData method is : 而LoadData方法是:

public void LoadData()
    {

        Custom = LoadCustomLocations();
        IsDataLoaded = true;

    }

    private LocationGroup LoadCustomLocations()
    {
        string dataFromAppSettings;
        LocationGroup data;

        if (IsolatedStorageSettings.ApplicationSettings.TryGetValue(CustomKey, out dataFromAppSettings))
        {
            data = JsonConvert.DeserializeObject<LocationGroup>(dataFromAppSettings);
        }
        else
        { 
            data = new LocationGroup();
        }

        return data;
    }

So, can anyone help ? 那么,有人可以帮忙吗?

In the case you described on your comment I think you set the DataContext to your Items. 就您在评论中描述的情况而言,我认为您将DataContext设置为Items。 When you create a new Items-List the DataContext will be lost. 当您创建新的Items-List时,DataContext将丢失。 So you have to reset the DataContext to the new loaded Items-List 因此,您必须将DataContext重置为新加载的Items-List

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM