InputEventRouter improvements

Oct 28, 2014 at 6:40 PM
Proposed changes to remove magic strings and reflection. You have:
Call("OnScrollChanged", e);
Which is implemented as
private void Call(string methodName, object e)
        {
            foreach (var inputSubscriber in inputSubscribers)
            {
                // initialize provider
                inputSubscriber.IsLeftButtonDown = isLeftMouseButtonDown;

                MethodInfo methodInfo = typeof(InputSubscriberBase).GetMethod(methodName, BindingFlags.NonPublic | BindingFlags.Instance);
                methodInfo.Invoke(inputSubscriber, new object[] { e });

                RoutedEventArgs re = e as RoutedEventArgs;
                if (re != null && re.Handled) break;
            }
        }
Ugh. How about using delegates?
private void OnScrollChanged(object sender, ScrollChangedEventArgs e)
        {
            Call((sub, args) => sub.OnScrollChanged((ScrollChangedEventArgs) args), e);
        }

        private void OnMouseDown(object sender, MouseButtonEventArgs e)
        {
            Mouse.Capture(_TreeView, CaptureMode.SubTree);
            _isLeftMouseButtonDown = true;
            _mouseDownPoint = e.GetPosition(_TreeView);
            Call((sub, args) => sub.OnMouseDown((MouseButtonEventArgs) args), e);
        }

        private void OnMouseMove(object sender, MouseEventArgs e)
        {
            _isLeftMouseButtonDown = e.LeftButton == MouseButtonState.Pressed;
            Call((sub, args) => sub.OnMouseMove((MouseEventArgs) args), e);
        }

        private void OnMouseUp(object sender, MouseButtonEventArgs e)
        {
            Call((sub, args) => sub.OnMouseUp((MouseButtonEventArgs) args), e);
            _isLeftMouseButtonDown = false;
            Mouse.Capture(null);
        }

        private void Call(Action<InputSubscriberBase, EventArgs> handle, EventArgs e)
        {
            foreach (var inputSubscriber in _inputSubscribers)
            {
                inputSubscriber.IsLeftButtonDown = _isLeftMouseButtonDown;
                handle(inputSubscriber, e);
                var re = e as RoutedEventArgs;
                if (re != null && re.Handled) break;
            }
        }
Completely removes reflection based upon strings.