简体   繁体   English

如何从Windows Phone 8 App C#的列表中删除项目

[英]how to remove a item from list in Windows Phone 8 App C#

When the user taps on the particular item in the list new page opens and there he can see full details of the particular item. 当用户点击列表中的特定项目时,将打开新页面,在那里他可以看到该特定项目的完整详细信息。 He can delete particular item if he thinks he don't want that. 如果他认为自己不想这样做,则可以删除特定项目。 My problem is the file was not deleting from the list. 我的问题是文件没有从列表中删除。

public partial class DetailsPage : PhoneApplicationPage
{
    SavedData prkdata = new SavedData();
    SavedDataList parkinglistobj = new SavedDataList();
    IsolatedStorageFile Settings = IsolatedStorageFile.GetUserStoreForApplication();
    int index;

    public DetailsPage()
    {
        InitializeComponent();        
        //this.Loaded += MainPage_Loaded;
    }        

    protected override void OnNavigatedTo(NavigationEventArgs e)
    {              
         string selectedIndex = "";

        if (NavigationContext.QueryString.TryGetValue("selectedItem", out selectedIndex))
         {
             index = int.Parse(selectedIndex);
             try
             {
                 parkinglistobj.Clear();

                 if (Settings.FileExists("CardItemList"))
                 {
                     using (IsolatedStorageFileStream fileStream = Settings.OpenFile("CardItemList", FileMode.Open))
                     {
                         DataContractSerializer serializer = new DataContractSerializer(typeof(SavedDataList));
                         parkinglistobj = (SavedDataList)serializer.ReadObject(fileStream);
                     }
                 }

                 ParkListBox.ItemsSource = parkinglistobj.Where(i => i.ID == index).ToList();

                 //String itemselected = parkinglistobj.Where(i => i.ID == index).Select(i.CardName).ToString();
                 //MessageBox.Show(itemselected);
             }
             catch
             {
                 //
             }
         }
    }

    private void DeleteFile(object sender, EventArgs e)
    {
        //ParkListBox.SelectedItem = parkinglistobj.Select(i => i.ID == index);
        //SavedData item = ParkListBox.DataContext as SavedData;
        SavedData item = parkinglistobj.Select(i => i.ID == index) as SavedData;
        //SavedData item = ParkListBox.SelectedItem as SavedData;
        parkinglistobj.Remove(item);
        MessageBox.Show("Deleted Successfully");
    }
}

在DeleteFile事件处理程序中,您应该删除该项目,然后将其重新序列化回CardItemList文件。

You to need to communicate back to your View (which is your ListBox ) that something has changed so it needs to update/redraw its Content. 您需要将已更改的内容传达回您的View(即ListBox ),因此需要更新/重绘其Content。

Unforunately, a generic List<T> will not automatically do this for you. 不幸的是,通用List<T>不会自动为您执行此操作。 What you need is to use an ObservableCollection<T> instead. 您需要使用ObservableCollection<T>代替。

MSDN: ObservableCollection Class , pay attention to this part of the description: MSDN:ObservableCollection Class ,请注意以下部分的描述:

Represents a dynamic data collection that provides notifications when items get added, removed, or when the whole list is refreshed. 表示动态数据集合,在添加,删除项目或刷新整个列表时提供通知。

If you used it correctly any addition and deletion to the collection will result in the ListBox redrawing itself. 如果正确使用它,则对集合的任何添加和删除都将导致ListBox重新绘制自身。

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

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