예제 #1
0
    private static bool TryDeserializeStandardWebEventArgs(
        string eventName,
        JsonElement eventArgsJson,
        [NotNullWhen(true)] out EventArgs?eventArgs)
    {
        // For back-compatibility, we recognize the built-in list of web event names and hard-code
        // rules about the deserialization type for their eventargs. This makes it possible to declare
        // an event handler as receiving EventArgs, and have it actually receive a subclass at runtime
        // depending on the event that was raised.
        //
        // The following list should remain in sync with EventArgsFactory.ts.

        switch (eventName)
        {
        case "input":
        case "change":
            // Special case for ChangeEventArgs because its value type can be one of
            // several types, and System.Text.Json doesn't pick types dynamically
            eventArgs = ChangeEventArgsReader.Read(eventArgsJson);
            return(true);

        case "copy":
        case "cut":
        case "paste":
            eventArgs = ClipboardEventArgsReader.Read(eventArgsJson);
            return(true);

        case "drag":
        case "dragend":
        case "dragenter":
        case "dragleave":
        case "dragover":
        case "dragstart":
        case "drop":
            eventArgs = DragEventArgsReader.Read(eventArgsJson);
            return(true);

        case "focus":
        case "blur":
        case "focusin":
        case "focusout":
            eventArgs = FocusEventArgsReader.Read(eventArgsJson);
            return(true);

        case "keydown":
        case "keyup":
        case "keypress":
            eventArgs = KeyboardEventArgsReader.Read(eventArgsJson);
            return(true);

        case "contextmenu":
        case "click":
        case "mouseover":
        case "mouseout":
        case "mousemove":
        case "mousedown":
        case "mouseup":
        case "dblclick":
            eventArgs = MouseEventArgsReader.Read(eventArgsJson);
            return(true);

        case "error":
            eventArgs = ErrorEventArgsReader.Read(eventArgsJson);
            return(true);

        case "loadstart":
        case "timeout":
        case "abort":
        case "load":
        case "loadend":
        case "progress":
            eventArgs = ProgressEventArgsReader.Read(eventArgsJson);
            return(true);

        case "touchcancel":
        case "touchend":
        case "touchmove":
        case "touchenter":
        case "touchleave":
        case "touchstart":
            eventArgs = TouchEventArgsReader.Read(eventArgsJson);
            return(true);

        case "gotpointercapture":
        case "lostpointercapture":
        case "pointercancel":
        case "pointerdown":
        case "pointerenter":
        case "pointerleave":
        case "pointermove":
        case "pointerout":
        case "pointerover":
        case "pointerup":
            eventArgs = PointerEventArgsReader.Read(eventArgsJson);
            return(true);

        case "wheel":
        case "mousewheel":
            eventArgs = WheelEventArgsReader.Read(eventArgsJson);
            return(true);

        case "toggle":
            eventArgs = EventArgs.Empty;
            return(true);

        default:
            // For custom event types, there are no built-in rules, so the deserialization type is
            // determined by the parameter declared on the delegate.
            eventArgs = null;
            return(false);
        }
    }
예제 #2
0
    public void Read_Works()
    {
        // Arrange
        var args = new TouchEventArgs
        {
            AltKey         = false,
            CtrlKey        = true,
            MetaKey        = true,
            ShiftKey       = false,
            Type           = "type1",
            ChangedTouches = new[]
            {
                new TouchPoint
                {
                    ClientX    = 1.3,
                    ClientY    = 2.9,
                    Identifier = 11,
                    PageX      = 7.0,
                    PageY      = 0.23,
                    ScreenX    = 1.0,
                    ScreenY    = 39.2
                },
            },
            Detail        = 789,
            TargetTouches = new[]
            {
                new TouchPoint
                {
                    ClientX    = 2.3,
                    ClientY    = 22.9,
                    Identifier = 8,
                    PageX      = 786.0,
                    PageY      = 1.4,
                    ScreenX    = 5.0,
                    ScreenY    = 9.2
                },
                new TouchPoint
                {
                    ClientX    = 12.3,
                    ClientY    = 2.9,
                    Identifier = 17,
                    PageX      = 6.3,
                    PageY      = 13.4,
                    ScreenX    = 4.0,
                    ScreenY    = 7.2
                },
            },
            Touches = new[]
            {
                new TouchPoint
                {
                    ClientX    = 6.8,
                    ClientY    = 1.9,
                    Identifier = 3,
                    PageX      = 2.3,
                    PageY      = 3.4,
                    ScreenX    = 4.1,
                    ScreenY    = 8.0,
                },
            },
        };

        var jsonElement = GetJsonElement(args);

        // Act
        var result = TouchEventArgsReader.Read(jsonElement);

        // Assert
        Assert.Equal(args.AltKey, result.AltKey);
        Assert.Equal(args.CtrlKey, result.CtrlKey);
        Assert.Equal(args.MetaKey, result.MetaKey);
        Assert.Equal(args.ShiftKey, result.ShiftKey);
        Assert.Equal(args.Type, result.Type);
        Assert.Equal(args.Detail, result.Detail);

        AssertEqual(args.Touches, result.Touches);
        AssertEqual(args.ChangedTouches, result.ChangedTouches);
        AssertEqual(args.TargetTouches, result.TargetTouches);
    }