2015-09-05 45 views
0

我使用Visual Studio(C#和XAML)創建了一個計算器。我希望能夠輸入數字和操作來進行計算。而不必點擊結果屏幕(就像Windows默認計算器一樣)。我怎樣才能做到這一點?我正在使用MVVM概念創建計算器,就像練習MVVM一樣。使用鍵盤作爲XAML和C#計算器的輸入

回答

1

您可以簡單地使用KeyUp事件來捕獲用戶輸入的密鑰。

將它添加到您的XAML窗口:

KeyUp="Window_KeyUp" 

private void Window_KeyUp(object sender, KeyEventArgs e) 
    { 
     string str= e.Key.ToString(); 
     //do whateveer you want with that. 

    } 

古德勒克。

2

您將希望在您的XAML中使用KeyBinding並將命令綁​​定到該密鑰。

<Window.InputBindings> 
    <KeyBinding Key="D1" 
       Command="AddOneToEntryCommand" /> 
    <KeyBinding Key="Add" 
       Command="AddCommand" /> 
    <KeyBinding Key="Subtract" 
       Command="SubtractCommand" /> 
    <!-- Add bindings for all the keys and handle the logic in your commands --> 
</Window.InputBindings>