Frage

I am working on WPF MVVM Application. I have a view (Employee) shown inside Main Region this view (Employee) contain scoped regions within it where I show/display Employee details views. Its working fine till this place New, display update and delete

But I am facing strange problem with New Operation If I create view for first time and click on new , Object got initialized my Data Object CurrentEmployee. But If I load some previous data its been shown properly and then I click on New, my Data Object CurrentEmployee took too much time and finaly crash. the problem so far traced is in OnPropertyChange

Thanks any sort of help/suggestion is highly appriciated

whole code of view model

[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

}
War es hilfreich?

Lösung

Finally solve the problem. I was using single Object CurrentTB_EMPLOYEE as current data for my form and ActiveData Item of another List view. this view model handle both form and list views. I just remove CurrentTB_EMPLOYEE from ActiveData Item to my list view and bind them through another object.. when CurrentTB_EMPLOYEE become responsible only for form its start working properly. Thanks every one for your precious time.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top