在财产变化上花了太多时间,最后崩溃申请

时间:2013-09-19 08:03:22

标签: c# wpf mvvm prism-4

我正在研究WPF MVVM应用程序。 我在主区域内显示了一个视图(Employee) 此视图(Employee)包含其中显示/显示Employee详细信息视图的范围区域。它工作正常,直到这个地方新,显示更新和删除

但我面临新手术的奇怪问题 如果我第一次创建视图并单击new,则Object初始化了我的Data Object CurrentEmployee。 但是,如果我加载了一些以前的数据,它已经正确显示,然后我点击New,我的数据对象CurrentEmployee花了太多时间并最终崩溃。到目前为止所追踪的问题是OnPropertyChange

感谢任何形式的帮助/建议高度评价

整个视图模型代码

[Export(typeof(ITB_EMPLOYEEViewModel))]
public class TB_EMPLOYEEViewModel : ViewModelBase, ITB_EMPLOYEEViewModel
{
    private TB_EMPLOYEE _currentTB_EMPLOYEE;
    IRegionManager RefRegionManager;
    IEventAggregator _eventAggregator;

    [ImportingConstructor]
    public TB_EMPLOYEEViewModel(IRegionManager regionManager, IEventAggregator eventAggregator)
        : base(regionManager, eventAggregator)
    {
        RefRegionManager = regionManager;
        HeaderInfo = "TB_EMPLOYEE";
        _eventAggregator = eventAggregator;
        OpenImageDialog = new DelegateCommand(OpenDialog, CanOpenDialog);
        //CurrentTB_EMPLOYEE = new TB_EMPLOYEE();
        //empHistoryVM = new TB_EMPLOYEE_HISTORYViewModel();
        //UnLoadChild();
        //LoadChild();
        New();
    }

    private void LoadChild()
    {
        IRegionManager regionManager = ServiceLocator.Current.GetInstance<IRegionManager>();
        if (regionManager.Regions.ContainsRegionWithName(RegionNames.EmployeeDetail))
        {
            IRegion region = regionManager.Regions[RegionNames.EmployeeDetail];

            var empHistory = ServiceLocator.Current.GetInstance<uTB_EMPLOYEE_HISTORYView>();
            if (region.GetView("EmployeeHistory") == null)// .Views.OfType<uTB_EMPLOYEE_HISTORYView>().SingleOrDefault() == null)
            {
                region.Add(empHistory, "EmployeeHistory");
            }
            if (CurrentTB_EMPLOYEE != null && CurrentTB_EMPLOYEE.ID!=0)
            {
                empHistoryVM = new TB_EMPLOYEE_HISTORYViewModel(CurrentTB_EMPLOYEE.ID);
            }
            else
            {
                empHistoryVM = new TB_EMPLOYEE_HISTORYViewModel();
            }
            empHistory.ViewModel = empHistoryVM;
            region.Activate(region.GetView("EmployeeHistory"));
        }
    }
    private void UnLoadChild()
    {
        IRegionManager regionManager = ServiceLocator.Current.GetInstance<IRegionManager>();
        if (regionManager.Regions.ContainsRegionWithName(RegionNames.EmployeeDetail))
        {
            IRegion region = regionManager.Regions[RegionNames.EmployeeDetail];
            if (region.GetView("EmployeeHistory") != null)// .Views.OfType<uTB_EMPLOYEE_HISTORYView>().SingleOrDefault() == null)
            {
                region.Remove(region.GetView("EmployeeHistory"));
            }
        }
    }
    #region DetailUserControls ViewModels
    private TB_EMPLOYEE_HISTORYViewModel empHistoryVM;
    #endregion DetailUserControls ViewModels
    #region Commands
    public DelegateCommand OpenImageDialog { get; set; }
    private bool CanOpenDialog() { return true; }
    public void OpenDialog()
    {
        using (OpenFileDialog objOpenFile = new OpenFileDialog())
        {
            if (objOpenFile.ShowDialog() == DialogResult.OK)
            {
                _currentTB_EMPLOYEE.PHOTO = Utility.ConvertImageToByte(objOpenFile.FileName);
                CurrentEmployeeImage = Utility.ConvertByteToImage(_currentTB_EMPLOYEE.PHOTO);
            }
        }


    }
    public override void ShowSelectedRow()
    {

        if (RefRegionManager.Regions[RegionNames.MainRegion].Views.OfType<uTB_EMPLOYEEView>().SingleOrDefault() == null)
        {
            RefRegionManager.RegisterViewWithRegion(RegionNames.MainRegion, typeof(uTB_EMPLOYEEView));
        }

        RefRegionManager.RequestNavigate(RegionNames.MainRegion, "uTB_EMPLOYEEView");
        UnLoadChild();
        LoadChild();
    }
    public override void RegisterCommands()
    {
        if (RefRegionManager.Regions[RegionNames.MainRegion].ActiveViews.OfType<uTB_EMPLOYEEView>().FirstOrDefault() != null)
        {
            if (RefRegionManager.Regions[RegionNames.MainRegion].ActiveViews.OfType<uTB_EMPLOYEEView>().FirstOrDefault().ToString() == "WPFApp.View.uTB_EMPLOYEEView")
            {
                GlobalCommands.ShowAllCommand.RegisterCommand(ShowAllCommand);
                GlobalCommands.SaveCommand.RegisterCommand(SaveCommand);
                GlobalCommands.NewCommand.RegisterCommand(NewCommand);
                GlobalCommands.DeleteCommand.RegisterCommand(DeleteCommand);
                GlobalCommands.CloseCommand.RegisterCommand(CloseCommand);
            }
        }
        if (RefRegionManager.Regions[RegionNames.MainRegion].ActiveViews.OfType<uListTB_EMPLOYEE>().FirstOrDefault() != null)
        {
            if (RefRegionManager.Regions[RegionNames.MainRegion].ActiveViews.OfType<uListTB_EMPLOYEE>().FirstOrDefault().ToString() == "WPFApp.ListView.uListTB_EMPLOYEE")
            {

                GlobalCommands.CloseCommand.RegisterCommand(CloseCommand);
                GlobalCommands.SearchCommand.RegisterCommand(SearchCommand);
                GlobalCommands.PrintCommand.RegisterCommand(PrintCommand);
                GlobalCommands.ExportToExcelCommand.RegisterCommand(ExportToExcelCommand);
                GlobalCommands.ExportToWordCommand.RegisterCommand(ExportToWordCommand);
                GlobalCommands.ExportToPDFCommand.RegisterCommand(ExportToPDFCommand);
            }
        }
    }
    public override bool CanShowAll()
    {
        return IsActive;
    }
    public override void ShowAll()
    {
        HeaderInfo = "TB_EMPLOYEE List";
        if (RefRegionManager.Regions[RegionNames.MainRegion].Views.OfType<uListTB_EMPLOYEE>().SingleOrDefault() == null)
        {
            RefRegionManager.RegisterViewWithRegion(RegionNames.MainRegion, typeof(uListTB_EMPLOYEE));
        }

        RefRegionManager.RequestNavigate(RegionNames.MainRegion, "uListTB_EMPLOYEE");
        UpdateListFromDB();
    }
    public override void UpdateListFromDB()
    {
        using (DBMain objDBMain = new DBMain())
        {
            TB_EMPLOYEEList = objDBMain.EntTB_EMPLOYEE.ToList<TB_EMPLOYEE>();
        }
    }
    public override void New()
    {          
        this.CurrentTB_EMPLOYEE = new TB_EMPLOYEE();
        UnLoadChild();         
        LoadChild();
    }
    public override void Close()
    {
        if (RefRegionManager.Regions[RegionNames.MainRegion].ActiveViews.OfType<uTB_EMPLOYEEView>().FirstOrDefault() != null)
        {
            if (RefRegionManager.Regions[RegionNames.MainRegion].ActiveViews.OfType<uTB_EMPLOYEEView>().FirstOrDefault().ToString() == "WPFApp.View.uTB_EMPLOYEEView")
            {
                RefRegionManager.Regions[RegionNames.MainRegion].Remove(RefRegionManager.Regions[RegionNames.MainRegion].ActiveViews.OfType<uTB_EMPLOYEEView>().FirstOrDefault());
                UnLoadChild();
            }
        }
        if (RefRegionManager.Regions[RegionNames.MainRegion].ActiveViews.OfType<uListTB_EMPLOYEE>().FirstOrDefault() != null)
        {
            if (RefRegionManager.Regions[RegionNames.MainRegion].ActiveViews.OfType<uListTB_EMPLOYEE>().FirstOrDefault().ToString() == "WPFApp.ListView.uListTB_EMPLOYEE")
            {
                RefRegionManager.Regions[RegionNames.MainRegion].Remove(RefRegionManager.Regions[RegionNames.MainRegion].ActiveViews.OfType<uListTB_EMPLOYEE>().FirstOrDefault());
            }
        }
    }
    public override void Delete()
    {
        if (RefRegionManager.Regions[RegionNames.MainRegion].ActiveViews.OfType<uTB_EMPLOYEEView>().FirstOrDefault() != null ||
                RefRegionManager.Regions[RegionNames.MainRegion].ActiveViews.OfType<uListTB_EMPLOYEE>().FirstOrDefault() != null)
        {
            if (CurrentTB_EMPLOYEE != null)
            {
                ConfirmationDialog confirmationMessage = new ConfirmationDialog("Do You want to Delete this Record of [TB_EMPLOYEE]", "Confirmation Dialog Box");
                confirmationMessage.AllowsTransparency = true;
                DoubleAnimation animFadeIn = new DoubleAnimation();
                animFadeIn.From = 0;
                animFadeIn.To = 1;
                animFadeIn.Duration = new Duration(TimeSpan.FromSeconds(1));
                confirmationMessage.BeginAnimation(Window.OpacityProperty, animFadeIn);
                confirmationMessage.ShowDialog();

                if (confirmationMessage.DialogValue)
                {
                    if (CurrentTB_EMPLOYEE.ID != 0)
                    {
                        using (DBMain objDBMain = new DBMain())
                        {
                            objDBMain.Entry<TB_EMPLOYEE>(CurrentTB_EMPLOYEE).State = EntityState.Deleted;
                            objDBMain.SaveChanges();
                            OnPropertyChanged("CurrentTB_EMPLOYEE");
                            CurrentTB_EMPLOYEE = null;
                        }
                    }
                    else
                    {
                        CurrentTB_EMPLOYEE = null;
                    }
                    UpdateListFromDB();
                }
            }
        }
    }
    public override bool CanSave()
    {
        return IsActive;
    }
    public override void Save()
    {
        if (RefRegionManager.Regions[RegionNames.MainRegion].ActiveViews.OfType<uTB_EMPLOYEEView>().FirstOrDefault() != null ||
               RefRegionManager.Regions[RegionNames.MainRegion].ActiveViews.OfType<uListTB_EMPLOYEE>().FirstOrDefault() != null)
        {
            if (CurrentTB_EMPLOYEE != null)
            {
                using (DBMain objDBMain = new DBMain())
                {
                    objDBMain.Entry<TB_EMPLOYEE>(CurrentTB_EMPLOYEE).State = CurrentTB_EMPLOYEE.ID == 0 ? EntityState.Added : EntityState.Modified;

                    foreach (TB_EMPLOYEE_HISTORY obj in empHistoryVM.TB_EMPLOYEE_HISTORYList)
                    {
                        objDBMain.Entry<TB_EMPLOYEE_HISTORY>(obj).State = EntityState.Added;
                        CurrentTB_EMPLOYEE.TB_EMPLOYEE_HISTORYList.Add(obj);
                        objDBMain.SaveChanges();                           
                    }
                }
                UpdateListFromDB();
            }
        }
    }
    #endregion Commands
    #region Properties
    private ImageSource _CurrentEmployeeImage;
    public ImageSource CurrentEmployeeImage { get { return _CurrentEmployeeImage; } private set { _CurrentEmployeeImage = value; OnPropertyChanged("CurrentEmployeeImage"); } }//OnPropertyChanged("CurrentTB_EMPLOYEE");
    public string CurrentEmployeeImagePath { get; set; }
    private List<TB_EMPLOYEE> _TB_EMPLOYEEList;
    public List<TB_EMPLOYEE> TB_EMPLOYEEList
    {
        get { return _TB_EMPLOYEEList; }
        set
        {
            _TB_EMPLOYEEList = value;
            RaisePropertyChanged();
        }
    }
    public TB_EMPLOYEE CurrentTB_EMPLOYEE
    {
        get { return _currentTB_EMPLOYEE; }
        set
        {
            _currentTB_EMPLOYEE = value;
            if (_currentTB_EMPLOYEE != null && _currentTB_EMPLOYEE.PHOTO != null)
            { CurrentEmployeeImage = Utility.ConvertByteToImage(_currentTB_EMPLOYEE.PHOTO); }
            //OnPropertyChanged("CurrentTB_EMPLOYEE");
            RaisePropertyChanged();
        }
    }
    private IList<TB_SETUP_RELIGION> _listRELIGION;
    public IList<TB_SETUP_RELIGION> listRELIGION
    {
        get
        {
            using (DBMain objDBMain = new DBMain())
            {
                _listRELIGION = objDBMain.EntTB_SETUP_RELIGION.ToList();
            }
            return _listRELIGION;
        }
    }
    private IList<string> _listTitles;
    public IList<string> listTitles
    {
        get
        {
            if (_listTitles == null)
            {
                _listTitles = new List<string>();
                _listTitles.Add("Mr");
                _listTitles.Add("Miss");
                _listTitles.Add("Mrs");
                //_listTitles.Add("Mr");
            }
            return _listTitles;
        }
    }
    private IList<TB_SETUP_GENDER> _listGENDER;
    public IList<TB_SETUP_GENDER> listGENDER
    {
        get
        {
            using (DBMain objDBMain = new DBMain())
            {
                _listGENDER = objDBMain.EntTB_SETUP_GENDER.ToList();
            }
            return _listGENDER;
        }
    }
    #endregion Properties
    #region FormNavigation
    private bool isActive;
    public override bool IsActive
    {
        get
        {
            return this.isActive;
        }
        set
        {
            isActive = value;
            OnIsActiveChanged();
        }
    }
    protected virtual void OnIsActiveChanged()
    {
        if (IsActive)
        {
            _eventAggregator.GetEvent<SubscribeToEvents>().Publish(new Dictionary<string, string>() { { "View", "TB_EMPLOYEE" }, { "FileName", @"Subscribtion to Events" } });
        }
        else
        {
            _eventAggregator.GetEvent<UnSubscribeToEvents>().Publish(new Dictionary<string, string>() { { "View", "TB_EMPLOYEE" }, { "FileName", @"UnSubscribtion to Events" } });
        }
        UnRegisterCommands();
        RegisterCommands();
    }
    public override bool IsNavigationTarget(NavigationContext navigationContext)
    {
        this.isActive = true;
        return this.isActive;
    }
    public override void OnNavigatedFrom(NavigationContext navigationContext)
    {
        UnRegisterCommands();
        this.isActive = false;
    }
    public override void OnNavigatedTo(NavigationContext navigationContext)
    {
        HeaderInfo = "TB_EMPLOYEE";
        this.isActive = true;
        RegisterCommands();
    }
    #endregion FormNavigation

}

1 个答案:

答案 0 :(得分:0)

最后解决问题。我使用单个Object CurrentTB_EMPLOYEE作为我的表单的当前数据和另一个List视图的ActiveData Item。此视图模型处理表单视图和列表视图。我只是从ActiveData Item中删除CurrentTB_EMPLOYEE到我的列表视图并将它们绑定到另一个对象..当CurrentTB_EMPLOYEE只负责表单开始正常工作时。感谢每一位宝贵的时间。