0
我有一个小UWP计算器应用程序,我想弄清楚如何将键盘号码绑定到按钮的单击事件x:绑定。在下面的情况下,我将内容“7”传递给ViewModel中的按钮RoutedEventArgs。理想情况下,我只需将键盘和数字键盘7绑定到XAML中的按钮,而不需要单独的事件处理程序。或者,我是否需要在事件处理程序后面使用代码,该程序将数字绑定到ViewModel的事件处理程序?如何将keydown绑定到按钮的单击事件UWP?
<Button x:Name="SevenButton"
Grid.Row="4"
Grid.Column="2"
Content="7"
Click="{x:Bind ViewModel.OnNumberBtnClick}"></Button>
//ViewModel
public void OnNumberBtnClick(object sender, RoutedEventArgs args)
{
if ((_outputValue) == "0" || (isOperationPerformed))
_outputValue = "";
isOperationPerformed = false;
Button numberBtnClick = (Button)sender;
if ((string)numberBtnClick.Content == ".")
{
if (!_outputValue.Contains("."))
{
OutputValue = OutputValue + numberBtnClick.Content;
}
}
else
{
OutputValue = OutputValue + numberBtnClick.Content;
}
}
//OnPropertyChanged
public string OutputValue
{
get
{
return _outputValue;
}
set
{
_outputValue = value;
OnPropertyChanged("OutputValue");
}
}
谢谢 马克
所以,以下的一些RareNCool的建议后,这是我来了一下。还有一些问题需要解决,比如专注于文本框,但其他方面都有效。
在我看来,我只是增加了一个绑定到视图模型KeyRoutedEventArgs事件:
<TextBlock x:Name="OutputValueTextBlock"
IsTextSelectionEnabled="True"
KeyDown="{x:Bind ViewModel.OnKeyboardNumberInput, Mode=OneWay}"
Text="{x:Bind ViewModel.OutputValue, Mode=TwoWay}"></TextBlock>
而在视图模型我创造了这个事件:
public void OnKeyboardNumberInput(object sender, KeyRoutedEventArgs e)
{
if (e.Key == VirtualKey.Number0 || e.Key == VirtualKey.NumberPad0 || (isOperationPerformed))
_outputValue = "";
if ((e.Key > VirtualKey.Number0 || e.Key < VirtualKey.Number9) || (e.Key > VirtualKey.NumberPad0 || e.Key < VirtualKey.NumberPad9))
{
string numberKey = e.Key.ToString();
if (e.Key == VirtualKey.Decimal)
{
isOperationPerformed = false;
if (!_outputValue.Contains("."))
{
// Switch if E.Key is a decimal and _output does not contains a decimal.
switch (numberKey)
{
case "Decimal":
numberKey = ".";
OutputValue = OutputValue + numberKey;
break;
}
}
}
else
{
// Swtich if E.Key is not a decimal.
switch (numberKey)
{
case "Number0":
case "NumberPad0":
numberKey = "0";
OutputValue = OutputValue + numberKey;
break;
case "Number1":
case "NumberPad1":
numberKey = "1";
OutputValue = OutputValue + numberKey;
break;
case "Number2":
case "NumberPad2":
numberKey = "2";
OutputValue = OutputValue + numberKey;
break;
case "Number3":
case "NumberPad3":
numberKey = "3";
OutputValue = OutputValue + numberKey;
break;
case "Number4":
case "NumberPad4":
numberKey = "4";
OutputValue = OutputValue + numberKey;
break;
case "Number5":
case "NumberPad5":
numberKey = "5";
OutputValue = OutputValue + numberKey;
break;
case "Number6":
case "NumberPad6":
numberKey = "6";
OutputValue = OutputValue + numberKey;
break;
case "Number7":
case "NumberPad7":
numberKey = "7";
OutputValue = OutputValue + numberKey;
break;
case "Number8":
case "NumberPad8":
numberKey = "8";
OutputValue = OutputValue + numberKey;
break;
case "Number9":
case "NumberPad9":
numberKey = "9";
OutputValue = OutputValue + numberKey;
break;
}
}
}
}
我真的很苦恼与键盘输入。一些示例代码会非常有帮助。 –
我已经采取了你的建议,并正在实施一个单独的功能,但我正在努力解决三个问题。 1如何使用ViewModel进行连线。 2.如何将VirtualKey枚举转换为字符串。 3如何在视图中设置焦点。 –
我已经创建了一个事件在视图模型像这样的:public void OnKeyboardNumberInput(对象发件人,KeyRoutedEventArgs E) { 如果(e.Key> VirtualKey.Number0 || VirtualKey.NumberPad0 || e.Key