0

我是 Windows Phone 开发的新手。我正在尝试从列表框中删除选定的项目。我有数据类

public class MyDataClass 
{ 
    public string MSG { get; set; } 
    public int Id { get; set; } 
}

然后我尝试删除所选项目(Button1_Click 事件)

MyDataClass item = MyDict.SelectedItem as MyDataClass; 
ObservableCollection dataList = new ObservableCollection(); 
dataList.Remove(item);

在任务中创建数据列表的问题,所以它对程序的其余部分不可用,如何改变这个?

    public async Task GETFROMDB()
    {
        int a = 1;
        Database database = new Database(ApplicationData.Current.LocalFolder, "DictData.db");
        await database.OpenAsync();
        string query = "SELECT * FROM MyDICT";
        Statement statement = await database.PrepareStatementAsync(query);
        statement.EnableColumnsProperty();
        ObservableCollection<MyDataClass> dataList = new ObservableCollection<MyDataClass>();
        while (await statement.StepAsync())
        {
            rawData = string.Format(statement.Columns["value"]);
            string[] sep = new string[] { "\r\n" }; //Splittng it with new line
            string[] arrData = rawData.Split(sep, StringSplitOptions.RemoveEmptyEntries);
            foreach (var d in arrData)
            {
                dataList.Add(new MyDataClass() { MSG = d, Id= a });
                a++;
            }
        }
        MyDict.ItemsSource = dataList;
    }
4

3 回答 3

0

您可以绑定到任务外部的数据列表并使数据列表静态或在任务中引用它吗?

创建列表时:

static ObservableCollection<MyDataClass> dataList = new ObservableCollection<MyDataClass>(); 
MyDict.ItemsSource = dataList;

然后在任务中:

public async Task GETFROMDB()
{
    int a = 1;
    Database database = new Database(ApplicationData.Current.LocalFolder, "DictData.db");
    await database.OpenAsync();
    string query = "SELECT * FROM MyDICT";
    Statement statement = await database.PrepareStatementAsync(query);
    statement.EnableColumnsProperty();
    while (await statement.StepAsync())
    {
        rawData = string.Format(statement.Columns["value"]);
        string[] sep = new string[] { "\r\n" }; //Splittng it with new line
        string[] arrData = rawData.Split(sep, StringSplitOptions.RemoveEmptyEntries);
        foreach (var d in arrData)
        {
            dataList.Add(new MyDataClass() { MSG = d, Id= a });
            a++;
        }
    }
}

然后在点击:

MyDataClass item = MyDict.SelectedItem as MyDataClass; 
dataList.Remove(item);

或者让它:

创建列表时:

ObservableCollection<MyDataClass> dataList = new ObservableCollection<MyDataClass>(); 
MyDict.ItemsSource = dataList;

然后在任务中:

public async Task GETFROMDB(ObservableCollection<MyDataClass> dataList)
{
    int a = 1;
    Database database = new Database(ApplicationData.Current.LocalFolder, "DictData.db");
    await database.OpenAsync();
    string query = "SELECT * FROM MyDICT";
    Statement statement = await database.PrepareStatementAsync(query);
    statement.EnableColumnsProperty();
    while (await statement.StepAsync())
    {
        rawData = string.Format(statement.Columns["value"]);
        string[] sep = new string[] { "\r\n" }; //Splittng it with new line
        string[] arrData = rawData.Split(sep, StringSplitOptions.RemoveEmptyEntries);
        foreach (var d in arrData)
        {
            dataList.Add(new MyDataClass() { MSG = d, Id= a });
            a++;
        }
    }
}

然后在点击:

 MyDataClass item = MyDict.SelectedItem as MyDataClass; 
 dataList.Remove(item);

当然,您需要等到任务完成。

于 2013-11-04T09:01:10.997 回答
0

您似乎正试图从一个全新的集合中删除该项目 - 而是尝试从您的列表框数据绑定到的那个中删除它。

于 2013-11-03T17:12:51.447 回答
0

尝试使用按钮数据上下文

像这样

在您的点击处理程序中

      private void ButtonClick(object sender, RoutedEventArgs e)
      {
         Button btn = sender as Button;
         if (btn != null)
          {
             MyDataClass item = btn.DataContext as MyDataClass; 
             dataList.Remove(item);
          }

        }
于 2013-11-03T19:11:08.753 回答