如何将ObservableCollection <t>另存为.txt,然后打开文件并再次在ObservableCollection <t>中对其进行转换?

时间:2018-11-01 14:25:48

标签: c# wpf

//这是一些代码,即时通讯在如何将值从字符串形式返回到ObservableCollection形式以及如何分隔该字符串列表的内容并将其归于属性方面存在一些问题任务类。如果有人可以帮助我,我会很棒

  public string liststrcomunication
    {
        get
        {
            List<Tasks> myList = new List<Tasks>(task);
            string liststr = string.Join(",", myList);
            return liststr;
        }
    }

public ObservableCollection<Tasks> strlistcomunication { get
        {
            List<string> result = (strlist.Split(new char[] { ',' })).ToList();
            List<Tasks> objParams = result.Select(p => new Tasks() { Name = result[0], Lead = result[1], MainTasks = result[2], StartDate = Convert.ToDateTime(result[3]), PercentDone = Convert.ToDouble(result[5]), QntDays = Convert.ToInt32(result[4]) }).ToList();
            ObservableCollection<Tasks> task = new ObservableCollection<Tasks>(objParams);
            return task;
        }
     }

private void opebutton_Click(object sender, RoutedEventArgs e)
    {
        OpenFileDialog openFileDialog = new OpenFileDialog();
        if (openFileDialog.ShowDialog() == true)
        {
            strlist = File.ReadAllText(openFileDialog.FileName);
            lvTasks.ItemsSource = strlistcomunication;
        }
    }

    private void savebutton_Click(object sender, RoutedEventArgs e)
    {
        SaveFileDialog saveFileDialog = new SaveFileDialog();
        saveFileDialog.Filter = "Text file (*.txt)|*.txt|C# file(*.cs)|*.cs";
        if (saveFileDialog.ShowDialog() == true)
            File.AppendAllText(saveFileDialog.FileName, liststrcomunication);

    }

public class Tasks
{
    public string Name { get; set; }
    public string Lead { get; set; }
    public string MainTasks { get; set; }
    public DateTime StartDate { get; set; }
    public int QntDays { get; set; }
    public double PercentDone { get; set; }
    public DateTime EndDate { get {return StartDate.AddDays(QntDays); } } 
}

0 个答案:

没有答案