How to create a KeyEventArgs object in WPF ( related to a SO answer )

前端 未结 5 1775
野趣味
野趣味 2020-12-29 04:39

I\'ve found this answer which look like what I need:

How can I programmatically generate keypress events in C#?

Except for the fact I can\'t create an insta

5条回答
  •  我在风中等你
    2020-12-29 05:08

    To unit test a viewmodel I had to use a combination of OscarRyz and Elijah W. Gagne answer to make this work.

        [TestMethod]
        public void method_event_expected()
        {
            this.objectUnderTest.TestMethod(
                new KeyEventArgs(Keyboard.PrimaryDevice, new HwndSource(0, 0, 0, 0, 0, "", IntPtr.Zero), 0, Key.Oem3)
                {
                    RoutedEvent = Keyboard.KeyDownEvent
                });
    
            Assert.IsTrue(...)
        }
    

    Keyboard.PrimaryDevice.ActiveSource was null so I had to fake it with a dummy window and then I also needed the RoutedEvent to be assigned.

提交回复
热议问题