Esempio n. 1
0
        public static void RegisterBindingEvents <T> (this T obj, List <EventBindingSettings> ebs)
            where T : class, IHandlers
        {
            var iEvent = (IHandlers)obj;

            foreach (var prop in typeof(T).GetProperties(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public))
            {
                var pn = prop.Name;
                var pt = prop.PropertyType;

                if (ControlUtil.ActionDictionary.ContainsKey(pt.Name))
                {
                    var eventName = ControlUtil.ActionDictionary [pt.Name];
                    if (!ebs.Any(x => x.BindingObject == prop.Name && x.EventName == eventName))
                    {
                        ControlUtil.HookUIControl(prop.GetValue(obj), obj);
                    }
                }

                var ce = prop.GetCustomAttribute(typeof(RegisterEvent));
                if (ce != null)
                {
                    var ceObj = (RegisterEvent)ce;
                    foreach (var eventName in ceObj.EventNames)
                    {
                        var control = prop.GetValue(obj);
                        var evClick = control.GetType().GetEvent(eventName);
                        if (evClick != null)
                        {
                            var tDelegate = evClick.EventHandlerType;
                            var miHandler = typeof(T).GetMethod("ControlsHandler", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public);
                            if (miHandler != null)
                            {
                                var             addHandler     = evClick.GetAddMethod();
                                var             d              = Delegate.CreateDelegate(tDelegate, obj, miHandler);
                                System.Object[] addHandlerArgs = { d };
                                addHandler.Invoke(control, addHandlerArgs);
                            }
                        }
                    }
                }
                var i = prop.PropertyType.GetInterface("INotifyPropertyChanged");
                if (i != null)
                {
                    var vm = (INotifyPropertyChanged)prop.GetValue(obj);
                    vm.PropertyChanged += iEvent.ViewEventHandler;
                    foreach (var vmProp in vm.GetType().GetProperties())
                    {
                        var ii = vmProp.PropertyType.GetInterface("INotifyCollectionChanged");
                        if (ii != null)
                        {
                            var collection = (INotifyCollectionChanged)vmProp.GetValue(vm);
                            collection.CollectionChanged += iEvent.ViewEventHandler;
                        }
                    }
                }
            }
        }
Esempio n. 2
0
        public static void RegisterEvents <T>(this T obj, List <EventBindingSettings> ebs)
            where T : UIViewController, IHandlers
        {
            var iEvent = (IHandlers)obj;

            foreach (var prop in typeof(T).GetProperties(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public))
            {
                var pn = prop.Name;
                var pt = prop.PropertyType;
                if (ControlUtil.ControlList.Contains(pt.Name))
                {
                    var ctrl = prop.GetValue(obj);
                    if (ctrl is UIControl)
                    {
                        ((UIControl)ctrl).AccessibilityIdentifier = prop.Name; //allows for TestCloud integration
                        ((UIControl)ctrl).AccessibilityLabel      = prop.Name;
                    }
                    if (!ebs.Any(x => x.BindingObject == pn))
                    {
                        ControlUtil.HookUIControl(ctrl, obj);
                    }
                }
                var ce = prop.GetCustomAttribute(typeof(RegisterEvent));
                if (ce != null)
                {
                    var ceObj = (RegisterEvent)ce;
                    foreach (var eventName in ceObj.EventNames)
                    {
                        var control = prop.GetValue(obj);
                        var evClick = control.GetType().GetEvent(eventName);
                        if (evClick != null)
                        {
                            var tDelegate = evClick.EventHandlerType;
                            var miHandler = typeof(T).GetMethod("ControlsHandler", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public);
                            if (miHandler != null)
                            {
                                var             addHandler     = evClick.GetAddMethod();
                                var             d              = Delegate.CreateDelegate(tDelegate, obj, miHandler);
                                System.Object[] addHandlerArgs = { d };
                                addHandler.Invoke(control, addHandlerArgs);
                            }
                        }
                    }
                }
                var i = prop.PropertyType.GetInterface("INotifyPropertyChanged");
                if (i != null)
                {
                    var vm = (INotifyPropertyChanged)prop.GetValue(obj);
                    vm.PropertyChanged += iEvent.ViewEventHandler;
                    foreach (var vmProp in vm.GetType().GetProperties(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public))
                    {
                        var ii = vmProp.PropertyType.GetInterface("INotifyCollectionChanged");
                        if (ii != null)
                        {
                            var collection = (INotifyCollectionChanged)vmProp.GetValue(vm);
                            collection.CollectionChanged += iEvent.ViewEventHandler;
                        }
                    }
                }
            }
        }