2016-06-13 82 views
0

我是WPF的新手,正在構建測試應用程序。在我的應用程序中,我有一個用戶控件調用另一個類來捕獲設備中的指紋。從設備捕獲指紋的類正在do while循環中運行,該循環不斷保持對設備的讀取。我想介紹一個可以打破這個做法的事件。在我的用戶控件中,我可以捕獲KeyDown事件。但是捕捉指紋的班級無法捕捉按鍵。我錯過了什麼?WPF子類捕獲按鍵事件

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; 
    } 

回答

-1

用戶控件由一個窗口和一個類,發時聚焦在用戶控件窗口,你按一個鍵,就可以得到一個onKeyDown事件。

我覺得你TNTFMT220類有沒有一個窗口,這樣你就可以生成用戶控件或在主窗口中的事件,並將其傳播方式不同的類,我建議你一對夫婦:

  1. 類物業

    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); 
    } 
    
  2. 類方法

    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); 
    
    } 
    

首先:

首先,如果需要的do while我大概再想想或者TNTFMT220當讀取數據可以生成的事件。

然後,如果你不希望do While會阻塞你的窗口,你必須在另一個線程中運行它。

+0

我試過以上,但得到同樣的問題。我會嘗試不同的線程方法。感謝您的洞察力 –