-2

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

但是我遇到了新操作的奇怪问题如果我第一次创建视图并单击 new ,对象会初始化我的数据对象 CurrentEmployee。但是如果我加载一些以前的数据,它会正确显示,然后我点击新建,我的数据对象 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

}
4

1 回答 1

0

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

于 2013-09-20T04:30:58.683 回答