Beispiel #1
0
        public T GetViewControl <T>(string vcKey = "") where T : UserControl, new()
        {
            T rst = null;

            if (ViewControls != null)
            {
                string typekey = GetViewControlKey(typeof(T), vcKey);
                if (ViewControls.ContainsKey(typekey) && ViewControls[typekey] != null)
                {
                    rst = ViewControls[typekey] as T;
                }
            }
            return(rst);
        }
        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);
                }
            }
        }
Beispiel #3
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();
            }
        }
Beispiel #4
0
        private T innerRegisterUI <T>(string vcKey) where T : new()
        {
            T      rst;
            string typekey = GetViewControlKey(typeof(T), vcKey);

            if (ViewControls.ContainsKey(typekey))
            {
                if (ViewControls[typekey] != null)
                {
                    rst = (T)ViewControls[typekey];
                }
                else
                {
                    rst = CreateUserControl <T>();
                    ViewControls[typekey] = rst;
                }
            }
            else
            {
                rst = CreateUserControl <T>();
                ViewControls.Add(typekey, rst);
            }
            return(rst);
        }