Пример #1
0
        private void UnRegisterRelationUserControl(ref UserControl uc, string vcKey, bool isUnRegisterVM)
        {
            if (uc == null)
            {
                return;
            }
            string uctypekey = GetViewControlKey(uc.GetType(), vcKey);
            var    ucchilds  = uc.Descendents();

            foreach (UserControl t in ucchilds.OfType <UserControl>())
            {
                UserControl t2    = t;
                string      t2key = GetViewControlKey(t2.GetType(), vcKey);
                UnRegisterRelationUserControl(ref t2, vcKey, isUnRegisterVM);
                if (t2 != null)
                {
                    if (t2.DataContext != null)
                    {
                        if (isUnRegisterVM)
                        {
                            if (t2.DataContext is ViewModelBase)
                            {
                                ViewModelLocator.Instance.UnRegisterViewModel(t2.DataContext.GetType(), vcKey);
                            }
                        }
                        t2.DataContext = null;
                    }
                    if (t2.Content != null)
                    {
                        GC.SuppressFinalize(t2.Content);
                        t2.Content = null;
                    }
                }
                if (ViewControls.ContainsKey(t2key))
                {
                    ViewControls.Remove(t2key);
                }
                if (t2 != null)
                {
                    GC.SuppressFinalize(t2);
                }
            }
        }
Пример #2
0
        private void InnerUnRegisterUI(Type type, string vcKey, bool isUnRegisterVM)
        {
            string vcTypeKey = GetViewControlKey(type, vcKey);

            if (ViewControls.ContainsKey(vcTypeKey))
            {
                object t = ViewControls[vcTypeKey];
                if (t != null)
                {
                    if (t is UserControl)
                    {
                        #region  usercontrol
                        UserControl uc = t as UserControl;
                        UnRegisterRelationUserControl(ref uc, vcKey, isUnRegisterVM);
                        if (uc.Content != null)
                        {
                            GC.SuppressFinalize(uc.Content);
                            uc.Content = null;
                        }
                        if (uc.DataContext != null)
                        {
                            if (isUnRegisterVM)
                            {
                                if (uc.DataContext is ViewModelBase)
                                {
                                    ViewModelLocator.Instance.UnRegisterViewModel(uc.DataContext.GetType(), vcKey);
                                }
                            }
                            uc.DataContext = null;
                        }
                        #endregion
                    }
                }
                ViewControls.Remove(vcTypeKey);
                if (t != null)
                {
                    GC.SuppressFinalize(t);
                }
                GC.Collect();
                GC.WaitForPendingFinalizers();
            }
        }