Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WPF子类捕获按键事件_C#_Wpf - Fatal编程技术网

C# WPF子类捕获按键事件

C# WPF子类捕获按键事件,c#,wpf,C#,Wpf,我是WPF的新手,正在构建一个测试应用程序。在我的应用程序中,有一个用户控件正在调用另一个类以从设备捕获指纹。从设备捕获指纹的类在do while循环上运行,该循环不断地从设备读取数据。我想介绍一个可以打破僵局的活动。在我的用户控件中,我可以捕获KeyDown事件。但是捕获指纹的类无法捕获按键。有什么我错过的吗 This is my Sample File.Xaml.cs code protected override void OnKeyDown(KeyEventArgs e)

我是WPF的新手,正在构建一个测试应用程序。在我的应用程序中,有一个用户控件正在调用另一个类以从设备捕获指纹。从设备捕获指纹的类在do while循环上运行,该循环不断地从设备读取数据。我想介绍一个可以打破僵局的活动。在我的用户控件中,我可以捕获KeyDown事件。但是捕获指纹的类无法捕获按键。有什么我错过的吗

This is my Sample File.Xaml.cs code

   protected override void OnKeyDown(KeyEventArgs e)
    {
        if (e.Key == Key.LeftCtrl || e.Key == Key.RightCtrl)
        {

        }
        else
            base.OnKeyDown(e);
    }

 private void button_Click(object sender, RoutedEventArgs e)
    {


            TNTFMT220 tntFmt220 = new TNTFMT220();
            string fingerPrintId = "";
            var data = tntFmt220.ContiniousCaptureFingerPrint(ref fingerPrintId);                          

    }
这是我捕获指纹的代码

   public bool ContiniousCaptureFingerPrint(ref string FingerPrintScanned)
    {
      do
       {
         //get data from device
        }  while(dataReturned);
     return true;
    }

UserControl由一个窗口和一个类组成,当聚焦UserControl窗口并按下一个键时,可以得到一个OnKeyDown事件

我认为您的
TNTFMT220
类没有窗口,因此您可以在UserControl或主窗口中生成事件,并以不同的方式将其传播到类,我建议您:

  • 类属性

    protected override void OnKeyDown(KeyEventArgs e)
    {
        if (e.Key == Key.LeftCtrl || e.Key == Key.RightCtrl)
        {
            tntFmt220.doStop= true;
        }
        else
            base.OnKeyDown(e);
    }
    
    private TNTFMT220 tntFmt220;
    
    private void button_Click(object sender, RoutedEventArgs e)
    {
        tntFmt220 = new TNTFMT220();
        string fingerPrintId = "";
        var data = tntFmt220.ContiniousCaptureFingerPrint(ref fingerPrintId);
    }
    
  • 类方法

    protected override void OnKeyDown(KeyEventArgs e)
    {
        if (e.Key == Key.LeftCtrl || e.Key == Key.RightCtrl)
        {
            tntFmt220.DoStop();
        }
        else
            base.OnKeyDown(e);
    }
    
    private TNTFMT220 tntFmt220;
    
    private void button_Click(object sender, RoutedEventArgs e)
    {
        tntFmt220 = new TNTFMT220();
        string fingerPrintId = "";
        var data = tntFmt220.ContiniousCaptureFingerPrint(ref fingerPrintId);
    
    }
    
  • 最重要的是:

    首先,我可能会再次思考,如果需要
    时执行
    ,或者
    TNTFMT220
    可以在读取数据时生成事件


    然后,如果您不想让
    执行,而
    会阻塞您的窗口,则必须在不同的线程中运行它。

    我尝试了上述方法,但遇到了相同的问题。我将尝试不同的线程方法。谢谢你的洞察力