示例#1
0
        internal string MakeDriverType(object control, out string nameSpace)
        {
            nameSpace = string.Empty;
            if (_typeFullNameAndWindowDriver.TryGetValue(control.GetType().FullName, out var info))
            {
                nameSpace = DriverCreatorUtils.GetTypeNamespace(info.DriverTypeFullName);
                return(DriverCreatorUtils.GetTypeName(info.DriverTypeFullName));
            }

            var name     = control.GetType().Name + DriverCreatorUtils.Suffix;
            var fullName = _selectedNamespace + "." + name;

            var nameList = new List <string>();

            foreach (var e in _typeFullNameAndWindowDriver)
            {
                nameList.Add(e.Value.DriverTypeFullName);
            }

            int index = 1;

            while (nameList.Contains(fullName))
            {
                name     = control.GetType().Name + DriverCreatorUtils.Suffix + index++;
                fullName = _selectedNamespace + "." + name;
            }

            _typeFullNameAndWindowDriver[control.GetType().FullName] = new WindowDriverInfo {
                DriverTypeFullName = fullName
            };
            return(name);
        }
示例#2
0
        static bool IsStopSearch(DependencyObject obj, bool stopWindowOrUserControl, bool stopControlDriver, int index)
        {
            if (stopWindowOrUserControl && (0 < index))
            {
                if (((obj is UserControl) || (obj is Page) || (obj is Window)))
                {
                    return(true);
                }
                var info = DriverCreatorUtils.GetDriverInfo(obj, DriverCreatorAdapter.TypeFullNameAndUserControlDriver);
                if (info != null)
                {
                    return(true);
                }
            }

            if (stopControlDriver)
            {
                var info = DriverCreatorUtils.GetDriverInfo(obj, DriverCreatorAdapter.TypeFullNameAndControlDriver);
                if (info != null && !info.SearchDescendantUserControls)
                {
                    return(true);
                }
            }
            return(false);
        }
 internal static void PickupChildren(UIElement ctrl)
 {
     foreach (UIElement e in GetChildren(ctrl))
     {
         var driver = DriverCreatorUtils.GetDriverTypeFullName(e, DriverCreatorAdapter.TypeFullNameAndControlDriver, DriverCreatorAdapter.TypeFullNameAndUserControlDriver, DriverCreatorAdapter.TypeFullNameAndWindowDriver, out var searchDescendantUserControls);
         if (!string.IsNullOrEmpty(driver))
         {
             DriverCreatorAdapter.AddDriverElements(e);
         }
     }
 }
示例#4
0
        private void CreateDriverInfoFindFromControlTree(DependencyObject target, DriverInfo <DependencyObject> driverInfo, List <ControlAndDefine> controlAndDefines, List <DependencyObject> mappedControls, List <string> names, string fileName)
        {
            var logical          = WPFUtility.GetLogicalTreeDescendants(target, true, true, 0);
            var visual           = WPFUtility.GetVisualTreeDescendants(target, true, true, 0);
            var logicalForGetter = WPFUtility.GetLogicalTreeDescendants(target, false, false, 0);
            var visualForGetter  = WPFUtility.GetVisualTreeDescendants(target, false, false, 0);

            foreach (var tree in new[] { logical, visual })
            {
                var cache = new BindingExpressionCache();
                foreach (var ctrl in tree)
                {
                    if (ReferenceEquals(ctrl, target))
                    {
                        continue;
                    }
                    //すでにマップされているかチェック
                    if (CollectionUtility.HasReference(mappedControls, ctrl))
                    {
                        continue;
                    }

                    //コントロールドライバ検索
                    var driver = DriverCreatorUtils.GetDriverTypeFullName(ctrl, DriverCreatorAdapter.TypeFullNameAndControlDriver, DriverCreatorAdapter.TypeFullNameAndUserControlDriver);
                    if (!string.IsNullOrEmpty(driver))
                    {
                        var name      = _customNameGenerator.MakeDriverPropName(ctrl, string.Empty, names);
                        var typeName  = DriverCreatorUtils.GetTypeName(driver);
                        var nameSpace = DriverCreatorUtils.GetTypeNamespace(driver);
                        var getter    = MakeCodeGetFromTree("Core.", logicalForGetter, visualForGetter, ctrl, cache, driverInfo.Usings, out var nogood);
                        var code      = $"public {typeName} {name} => {getter};";
                        if (nogood)
                        {
                            code += $" {TodoComment}";
                        }
                        controlAndDefines.Add(new ControlAndDefine(ctrl, name, code));
                        mappedControls.Add(ctrl);
                        DriverCreatorAdapter.AddCodeLineSelectInfo(fileName, getter, ctrl);
                        if (!driverInfo.Usings.Contains(nameSpace))
                        {
                            driverInfo.Usings.Add(nameSpace);
                        }
                    }
                }
            }
        }
        public string[] GetAttachExtensionClassCandidates(object obj)
        {
            var candidates = new List <string>();
            var parent     = VisualTreeHelper.GetParent((DependencyObject)obj);

            while (parent != null)
            {
                var driver = DriverCreatorUtils.GetDriverTypeFullName(parent, new Dictionary <string, ControlDriverInfo>(),
                                                                      DriverCreatorAdapter.TypeFullNameAndUserControlDriver,
                                                                      DriverCreatorAdapter.TypeFullNameAndWindowDriver, out var _);
                if (!string.IsNullOrEmpty(driver))
                {
                    candidates.Add(driver);
                }
                parent = VisualTreeHelper.GetParent(parent);
            }
            candidates.Add(WindowsAppFriendTypeFullName);

            //Asでのアタッチ用に足しておく
            {
                candidates.AddRange(GetDriverTypeCandidates((DependencyObject)obj));
            }
            return(candidates.ToArray());
        }
示例#6
0
 private static ControlAndFieldName <DependencyObject>[] GetFields(object obj)
 => DriverCreatorUtils.GetFields <DependencyObject>(obj, typeof(Window), typeof(UserControl), typeof(Page), typeof(DependencyObject));
示例#7
0
        private DriverInfo <DependencyObject> CreateDriverInfo(DependencyObject targetControl, string fileName)
        {
            var driverInfo = new DriverInfo <DependencyObject>(targetControl);

            var mappedControls = new List <DependencyObject>();
            var names          = new List <string>();
            var ancesters      = WPFUtility.GetVisualTreeAncestor(targetControl);

            var controlAndDefines = new List <ControlAndDefine>();

            //フィールドから検索
            foreach (var e in GetFields(targetControl))
            {
                //たまに親を持っているのがいるのではじく
                if (CollectionUtility.HasReference(ancesters, e.Control))
                {
                    continue;
                }

                //不正なフィールド名のものは取得できない
                if (!_dom.IsValidIdentifier(e.Name))
                {
                    continue;
                }

                //すでにマップされているかチェック
                if (CollectionUtility.HasReference(mappedControls, e.Control))
                {
                    continue;
                }

                //コントロールドライバ
                var driver = DriverCreatorUtils.GetDriverTypeFullName(e.Control, DriverCreatorAdapter.TypeFullNameAndControlDriver, DriverCreatorAdapter.TypeFullNameAndUserControlDriver);
                if (!string.IsNullOrEmpty(driver))
                {
                    mappedControls.Add(e.Control);
                    var name      = _customNameGenerator.MakeDriverPropName(e.Control, e.Name, names);
                    var typeName  = DriverCreatorUtils.GetTypeName(driver);
                    var nameSpace = DriverCreatorUtils.GetTypeNamespace(driver);
                    var key       = $"Core.Dynamic().{e.Name}";
                    controlAndDefines.Add(new ControlAndDefine(e.Control, name, $"public {typeName} {name} => {key};"));
                    DriverCreatorAdapter.AddCodeLineSelectInfo(fileName, key, e.Control);
                    if (!driverInfo.Usings.Contains(nameSpace))
                    {
                        driverInfo.Usings.Add(nameSpace);
                    }
                }
                //ユーザーコントロールドライバ
                else if (((e.Control is UserControl) && (e.Control.GetType() != typeof(UserControl))) ||
                         ((e.Control is Page) && (e.Control.GetType() != typeof(Page))))
                {
                    mappedControls.Add(e.Control);
                    var name     = _customNameGenerator.MakeDriverPropName(e.Control, e.Name, names);
                    var typeName = _driverTypeNameManager.MakeDriverType(e.Control, out var nameSpace);
                    var key      = $"Core.Dynamic().{e.Name}";
                    controlAndDefines.Add(new ControlAndDefine(e.Control, name, $"public {typeName} {name} => {key};"));
                    if (!string.IsNullOrEmpty(nameSpace) && (nameSpace != DriverCreatorAdapter.SelectedNamespace) && !driverInfo.Usings.Contains(nameSpace))
                    {
                        driverInfo.Usings.Add(nameSpace);
                    }
                    DriverCreatorAdapter.AddCodeLineSelectInfo(fileName, key, e.Control);
                }
            }

            //フィールド上に現れないオブジェクトを検索
            CreateDriverInfoFindFromControlTree(targetControl, driverInfo, controlAndDefines, mappedControls, names, fileName);

            //Sortのロジックがイマイチわかっていない。念のため
            try
            {
                // LogicalTree順のコントロールリスト取得
                var controlList = WPFUtility.GetLogicalTreeDescendants(targetControl, true, true, 0);

                // フィールドをタブオーダーでソート
                controlAndDefines.Sort((l, r) => controlList.IndexOf(l.Control) - controlList.IndexOf(r.Control));
            }
            catch { }

            //コンテキストメニュー特別処理
            foreach (var e in controlAndDefines)
            {
                driverInfo.Members.Add(e.Define);
                var frameworkElement = e.Control as FrameworkElement;
                if (frameworkElement != null && frameworkElement.ContextMenu != null)
                {
                    var core = (frameworkElement is Window || frameworkElement is UserControl || frameworkElement is Page) ?
                               ".Core" : string.Empty;
                    var code = $"public WPFContextMenu {e.Name}ContextMenu => new WPFContextMenu{{Target = {e.Name}{core}.AppVar}};";
                    driverInfo.Members.Add(code);
                }
            }

            return(driverInfo);
        }
示例#8
0
        private void GetAllWindowAndUserControl(bool isControlTreeOnly, DependencyObject control, Dictionary <Type, DependencyObject> targets, List <Type> getFromControlTreeOnly, List <DependencyObject> recursiveCheck)
        {
            if (control == null)
            {
                return;
            }

            //ルート以外はすでに割り当てがあれば再生成しないようにする
            var addToTarget = true;

            if (DriverCreatorUtils.GetDriverInfo(control, DriverCreatorAdapter.TypeFullNameAndControlDriver) != null ||
                DriverCreatorUtils.GetDriverInfo(control, DriverCreatorAdapter.TypeFullNameAndWindowDriver) != null ||
                DriverCreatorUtils.GetDriverInfo(control, DriverCreatorAdapter.TypeFullNameAndUserControlDriver) != null)
            {
                addToTarget = false;
            }

            //再帰チェック
            if (CollectionUtility.HasReference(recursiveCheck, control))
            {
                return;
            }
            recursiveCheck.Add(control);

            if ((control is Window) ||
                ((control is UserControl) && (control.GetType() != typeof(UserControl))) ||
                ((control is Page) && (control.GetType() != typeof(Page))))
            {
                if (addToTarget)
                {
                    targets[control.GetType()] = control;
                    if (isControlTreeOnly)
                    {
                        getFromControlTreeOnly.Add(control.GetType());
                    }
                }

                //Form, UserControlの時はメンバも見る
                foreach (var e in GetFields(control))
                {
                    //まれにGridなどををメンバに持っている場合がある。
                    if (e.Control.GetType().Assembly == typeof(Grid).Assembly)
                    {
                        continue;
                    }

                    GetAllWindowAndUserControl(false, e.Control, targets, getFromControlTreeOnly, recursiveCheck);
                }
            }
            if (!(control is FrameworkElement))
            {
                return;
            }

            foreach (var e in WPFUtility.GetLogicalTreeDescendants(control, true, true, 0))
            {
                GetAllWindowAndUserControl(true, e, targets, getFromControlTreeOnly, recursiveCheck);
            }
            foreach (var e in WPFUtility.GetVisualTreeDescendants(control, true, true, 0))
            {
                GetAllWindowAndUserControl(true, e, targets, getFromControlTreeOnly, recursiveCheck);
            }
        }
 static string[] GetDriverTypeCandidates(DependencyObject elementCtrl)
 => DriverCreatorUtils.GetDriverTypeFullNames(elementCtrl, DriverCreatorAdapter.MultiTypeFullNameAndControlDriver, DriverCreatorAdapter.MultiTypeFullNameAndUserControlDriver, DriverCreatorAdapter.MultiTypeFullNameAndWindowDriver);