如何将多个触发器事件方法从触发器事件if else if语句重组为新的单个if el if语句WPF Dispatcher.

ttisahbt  于 2023-08-07  发布在  其他
关注(0)|答案(1)|浏览(92)

我正试图修改一个很长的脚本,并希望在开始之前的意见。
我最初的结构如下:

namespace MyMainNamespace.MySubNamespace.Namespaces
{
    
    public class MyClass : Class
    {

        private async void MyMethod(object sender, System.Windows.Input.KeyEventArgs e)
        {
            await MyControl.Dispatcher.InvokeAsync((Action) (() =>

                /// GROUP A
                if(Keyboard.IsKeyDown(Key.PageUp) && Keyboard.IsKeyDown(Key.A))
                {

                    TriggerCustomEvent(MyEventA1 =>
                    {
                        
                        DoA1;
                        
                    }, null);
                
                
                    TriggerCustomEvent(MyEventA2 =>
                    {
                        DoA2;
                        
                    }, null);
                }

                /// GROUP REVERSE A
                else if(Keyboard.IsKeyDown(Key.PageDown) && Keyboard.IsKeyDown(Key.A))
                {

                    TriggerCustomEvent(MyEventReverseA1 =>
                    {
                        
                        DoReverseA1;
                        
                    }, null);
                
                
                    TriggerCustomEvent(MyEventReverseA2 =>
                    {
                        DoReverseA2;
                        
                    }, null);
                }

                /// GROUP B
                if(Keyboard.IsKeyDown(Key.PageUp) && Keyboard.IsKeyDown(Key.B))
                {

                    TriggerCustomEvent(MyEventB1 =>
                    {
                        
                        DoB1;
                        
                    }, null);
                
                
                    TriggerCustomEvent(MyEventB2 =>
                    {
                        DoB2;
                        
                    }, null);
                }

                /// GROUP REVERSE B
                else if(Keyboard.IsKeyDown(Key.PageDown) && Keyboard.IsKeyDown(Key.B))
                {

                    TriggerCustomEvent(MyEventReverseB1 =>
                    {
                        
                        DoReverseB1;
                        
                    }, null);
                
                
                    TriggerCustomEvent(MyEventReverseB2 =>
                    {
                        DoReverseB2;
                        
                    }, null);
                }

            }));
                                            
            e.Handled = true;
        }
    }
}

字符串
基本上,一些 KeyDown 事件由2组 if else if 语句捕获,并执行一些触发器事件。
接下来我想做的是将多个事件组合成2个新的热键。
主要是,将 if else if 语句分成2组,如下所示:

1st group:

GROUP A
GROUP B
2nd group:

GROUP REVERSE A
GROUP REVERSE B

的数据
我想到了这个解决方案:
从触发器事件中删除 if else if 语句,然后将触发器事件放入方法中。
最后,创建一个新的 if else if 语句集,并在该新的 if else if 语句集中调用新的触发器事件方法。
我想到了这个脚本示例:

namespace MyMainNamespace.MySubNamespace.Namespaces
{
    
    public class MyClass : Class
    {

        private async void MyMethod(object sender, System.Windows.Input.KeyEventArgs e)
        {
            await MyControl.Dispatcher.InvokeAsync((Action) (() =>

                if(Keyboard.IsKeyDown(Key.End) && Keyboard.IsKeyDown(Key.NumPad1))
                {

                    MySingleHotkeysMethodA(); // Calling method GROUP A
                
                
                    MySingleHotkeysMethodB(); // Calling method GROUP B
                }

                else if(Keyboard.IsKeyDown(Key.Home) && Keyboard.IsKeyDown(Key.NumPad1))
                {

                    MySingleHotkeysReverseMethodA(); // Calling method GROUP REVERSE A
                
                
                    MySingleHotkeysReverseMethodB(); // Calling method GROUP REVERSE B
                }

                /// GROUP A
                public void MySingleHotkeysMethodA()
                {
                    TriggerCustomEvent(MyEventA1 =>
                    {
                        DoA1;
                        
                    }, null);
                
                
                    TriggerCustomEvent(MyEventA2 =>
                    {
                        DoA2;
                        
                    }, null);
                }

                /// GROUP REVERSE A
                public void MySingleHotkeysReverseMethodA()
                {
                    TriggerCustomEvent(MyEventReverseA1 =>
                    {
                        DoReverseA1;
                        
                    }, null);
                
                
                    TriggerCustomEvent(MyEventReverseA2 =>
                    {
                        DoReverseA2;
                        
                    }, null);
                }

                /// GROUP B
                public void MySingleHotkeysMethodB()
                {
                    TriggerCustomEvent(MyEventB1 =>
                    {
                        DoB1;
                        
                    }, null);
                
                
                    TriggerCustomEvent(MyEventB2 =>
                    {
                        DoB2;

                    }, null);
                }

                /// GROUP REVERSE B
                public void MySingleHotkeysReverseMethodB()
                {
                    TriggerCustomEvent(MyEventReverseB1 =>
                    {
                        DoReverseB1;
                        
                    }, null);
                
                
                    TriggerCustomEvent(MyEventReverseB2 =>
                    {
                        DoReverseB2;
                        
                    }, null);
                }

            }));
                                            
            e.Handled = true;
        }
    }
}


它看起来不错,但我必须承认,我对调度程序的这个用例还不熟悉,我非常感谢评论、评论甚至改进或其他方法建议,以便更好地开始在生产脚本中进行更改。

dpiehjr4

dpiehjr41#

有点生 rust 的方法位置在原来的职位。
现在我修改如下,似乎工作正常:

namespace MyMainNamespace.MySubNamespace.Namespaces
{
    
    public class MyClass : Class
    {
        /// GROUP A
        public void MySingleHotkeysMethodA()
        {
            TriggerCustomEvent(MyEventA1 =>
            {
                
                DoA1;
                
            }, null);
        
        
            TriggerCustomEvent(MyEventA2 =>
            {
                DoA2;
                
            }, null);
        }

        /// GROUP REVERSE A
        public void MySingleHotkeysReverseMethodA()
        {
            TriggerCustomEvent(MyEventReverseA1 =>
            {
                
                DoReverseA1;
                
            }, null);
        
        
            TriggerCustomEvent(MyEventReverseA2 =>
            {
                DoReverseA2;
                
            }, null);
        }

        /// GROUP B
        public void MySingleHotkeysMethodB()
        {
            TriggerCustomEvent(MyEventB1 =>
            {
                
                DoB1;
                
            }, null);
        
        
            TriggerCustomEvent(MyEventB2 =>
            {
                DoB2;
                
            }, null);
        }

        /// GROUP REVERSE B
        public void MySingleHotkeysReverseMethodB()
        {
            TriggerCustomEvent(MyEventReverseB1 =>
            {
                
                DoReverseB1;
                
            }, null);
        
        
            TriggerCustomEvent(MyEventReverseB2 =>
            {
                DoReverseB2;
                
            }, null);
        }

        private async void MyMethod(object sender, System.Windows.Input.KeyEventArgs e)
        {
            await MyControl.Dispatcher.InvokeAsync((Action) (() =>

                if(Keyboard.IsKeyDown(Key.End) && Keyboard.IsKeyDown(Key.NumPad1))
                {

                    MySingleHotkeysMethodA(); // Calling method GROUP A
                
                
                    MySingleHotkeysMethodB(); // Calling method GROUP B
                }

                else if(Keyboard.IsKeyDown(Key.Home) && Keyboard.IsKeyDown(Key.NumPad1))
                {

                    MySingleHotkeysReverseMethodA(); // Calling method GROUP REVERSE A
                
                
                    MySingleHotkeysReverseMethodB(); // Calling method GROUP REVERSE B
                }

            }));
                                            
            e.Handled = true;
        }
    }
}

字符串
将做一些更多的测试,应该是很好的解决生产脚本。

相关问题