示例#1
0
        /// <summary>
        /// 根据学生的ID删除学生
        /// </summary>
        /// <param name="id">学生ID</param>
        void DelStudentById(int id)
        {
            var model = StudentList.FirstOrDefault(s => s.Id == id);

            if (model != null)
            {
                StudentList.Remove(model);
            }
        }
        private async void ViewStudentEdit()
        {
            if (SelectedStudent == null)
            {
                await DialogHost.Show(new OkMessageDialog()
                {
                    DataContext = "Please select a student!"
                });

                return;
            }
            await DialogHost.Show(new PleaseWaitView(), "RootDialog",
                                  delegate(object sender, DialogOpenedEventArgs args)
            {
                Task.Run(() =>
                {
                    //_oldRequirementStudents = Requirements;
                    AddRequirementsToStudent(SelectedStudent);
                    //Requirements = _context.RequirementStudents.AsNoTracking()
                    //    .Where(c => c.StudentId == SelectedStudent.Id)
                    //    .ToList();
                    Requirements = _context.RequirementStudents
                                   .Where(c => c.StudentId == SelectedStudent.Id)
                                   .ToList();
                    Thread.Sleep(1000);
                }).ContinueWith((t, _) =>
                {
                    //Requirements = SelectedStudent.RequirementStudents.ToList();
                    _oldRequirementStudents = Requirements;

                    SelectedYearLevel = SelectedStudent.YearLevel;
                    SelectedSection   = SelectedStudent.Section;
                    YearLevels        = _context.YearLevels.ToObservableCollection();
                    GenderList        = Enum.GetNames(typeof(EnumGender)).ToList();

                    args.Session.UpdateContent(new StudentEditView()
                    {
                        DataContext = this
                    });
                }, null, TaskScheduler.FromCurrentSynchronizationContext());
            }, delegate(object sender, DialogClosingEventArgs args)
            {
                bool result = false;
                if (Equals(args.Parameter, false))
                {
                    return;
                }
                if (Equals(args.Parameter, "Close"))
                {
                    try
                    {
                        _context.Entry(SelectedStudent).CurrentValues.SetValues(_context.Entry(SelectedStudent).OriginalValues);
                        foreach (var requirement in Requirements)
                        {
                            _context.Entry(requirement).CurrentValues.SetValues(_context.Entry(requirement).OriginalValues);
                        }
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e);
                    }
                    Requirements = _oldRequirementStudents;
                    return;
                }
                if (_isOkMessageOpen && Equals(args.Parameter, "Cancel"))
                {
                    _isOkMessageOpen = false;
                    args.Cancel();
                    args.Session.UpdateContent(_content);
                }
                if (Equals(args.Parameter, "Update"))
                {
                    args.Cancel();
                    args.Session.UpdateContent(new PleaseWaitView());
                    Task.Run(() =>
                    {
                        Student student = null;
                        try
                        {
                            //using (var context = new MorenoContext())
                            //{
                            //    foreach (var item in Requirements)
                            //    {
                            //        context.Entry(item).State = EntityState.Modified;
                            //    }
                            //    context.SaveChanges();
                            //}
                            ////foreach (var requirement in Requirements)
                            ////{
                            ////    //_context.Entry(requirement).State = EntityState.Modified;
                            ////    _context.RequirementStudents.Attach(requirement);
                            ////}
                            ////_context.SaveChanges();
                            SelectedStudent.YearLevel = SelectedYearLevel;
                            SelectedStudent.Section   = SelectedSection;
                            student = StudentList.FirstOrDefault(c => c == SelectedStudent);

                            _context.SaveChanges();
                            result = true;
                        }

                        catch (Exception e)
                        {
                            Console.WriteLine(e.Message);
                            result = false;
                        }
                        return(student);
                    }).ContinueWith((t, _) =>
                    {
                        if (t.Result != null)
                        {
                            t.Result.RequirementStudents = Requirements;
                        }
                        LoadStudents();
                        var message = result ? "Update Successfull" : "Update Failed";
                        if (result)
                        {
                            LoadStudents();
                        }

                        args.Session.UpdateContent(new OkMessageDialog()
                        {
                            DataContext = message
                        });
                    }, null, TaskScheduler.FromCurrentSynchronizationContext());
                }
                if (Equals(args.Parameter, "Delete"))
                {
                    _content = args.Session.Content;
                    args.Cancel();
                    _isOkMessageOpen = true;
                    args.Session.UpdateContent(new OkCancelMessageDialog()
                    {
                        DataContext = $"Are you sure you want to delete {SelectedStudent.FullName}?"
                    });
                }
                if (Equals(args.Parameter, "Ok") && _isOkMessageOpen)
                {
                    _isOkMessageOpen = false;
                    args.Cancel();
                    args.Session.UpdateContent(new PleaseWaitView());
                    Task.Run(() =>
                    {
                        try
                        {
                            _context.Students.Remove(SelectedStudent);
                            _context.SaveChanges();
                            result = true;
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine(e.Message);
                            result = false;
                        }
                    }).ContinueWith((t, _) =>
                    {
                        LoadStudents();
                        var message      = result ? "Delete Successfull" : "Delete Failed";
                        _isOkMessageOpen = false;
                        args.Session.UpdateContent(new OkMessageDialog()
                        {
                            DataContext = message
                        });
                    }, null, TaskScheduler.FromCurrentSynchronizationContext());
                }
            });


            //Requirements = _context.RequirementStudents.AsNoTracking().Where(c => c.StudentId == SelectedStudent.Id)
            //    .ToList();
            //_oldRequirementStudents = Requirements;
            //await AddRequirementsToStudentAsync(SelectedStudent);
            //await DialogHost.Show(new StudentEditView() { DataContext = this }, "RootDialog", StudentEditClosingEventHandler);
        }