任何人都知道如何添加到另一个类的一个类中的ObservableCollection?我要做的是当您点击CurrentTasks
页面上的按钮时,它会在我CurrentTask
班级的completedTaskList
中添加新的CompletedTasks
当前任务页面
public partial class CurrentTasks : ContentPage
{
private ObservableCollection<Models.CurrentTask> currentTaskList { get; set; }
internal ObservableCollection<Models.CurrentTask> CurrentTasklist { get => currentTaskList; set => currentTaskList = value; }
public CurrentTasks()
{
currentTaskList = new ObservableCollection<Models.CurrentTask>()
{
new Models.CurrentTask {TaskName = "First Task", TaskDescription = "Very important", Prioity = ImageSource.FromResource("LifeManager.Images.high.png")},
new Models.CurrentTask {TaskName = "Second Task", TaskDescription = "Not as important", Prioity = ImageSource.FromResource("LifeManager.Images.low.png")}
};
InitializeComponent();
listView.ItemsSource = currentTaskList;
}
private void Button_Clicked(object sender, EventArgs e)
{
CurrentTask newtask = new CurrentTask { TaskName = "New task", TaskDescription = "New task Descroipti", Prioity = ImageSource.FromResource("LifeManager.Images.high.png") };
currentTaskList.Add(newtask);
}
}
已完成的任务
public partial class CompletedTasks
{
private ObservableCollection<Models.CurrentTask> completedTaskList { get; set; }
internal ObservableCollection<Models.CurrentTask> CompletedTaskList { get => CompletedTaskList; set => CompletedTaskList = value; }
public CompletedTasks()
{
InitializeComponent();
completedTaskList = new ObservableCollection<Models.CurrentTask>()
{
new Models.CurrentTask {TaskName = "First Task", TaskDescription = "Very important", Prioity = ImageSource.FromResource("LifeManager.Images.high.png")},
new Models.CurrentTask {TaskName = "Second Task", TaskDescription = "Not as important", Prioity = ImageSource.FromResource("LifeManager.Images.low.png")}
};
listView.ItemsSource = completedTaskList;
}
private void Button_Clicked(object sender, EventArgs e)
{
CurrentTask newtask = new CurrentTask { TaskName = "New task", TaskDescription = "New task Descroipti", Prioity = ImageSource.FromResource("LifeManager.Images.high.png") };
}
}
答案 0 :(得分:0)
我假设CompletedTasks对象在其他窗口中维护。下面的例子我假设为mainWindow。
在主窗口中,您可以维护CompletedTasks的对象
并具有返回对象的属性。
public partial class MainWindow : Window
{
private CompletedTasks comT;
public CompletedTasks completedTask
{
get { return comT; }
set { comT = value; }
}
..
..
..
..
}
现在在您的页面中,访问主窗口并更新列表。
如下所示。
public partial class CurrentTasks : ContentPage
{
....
....
....
....
private void Button_Clicked(object sender, EventArgs e)
{
CurrentTask newtask = new CurrentTask { TaskName = "New task", TaskDescription = "New task Descroipti", Prioity = ImageSource.FromResource("LifeManager.Images.high.png") };
currentTaskList.Add(newtask);
//update Here when task is done
//Access the main window
var mainWin = Application.Current.Windows
.Cast<Window>()
.FirstOrDefault(window => window is MainWindow) as MainWindow;
mainWin.completedTask.CompletedTaskList.Add(newtask);
}
}
例如我使用了Main Window。你可以使用任何窗口。