我正在为 Metro 应用程序开发 C# 和 XAML。我有一个文本框,我希望一旦在该文本框中按下输入,就会出现一个新的文本框。但我得到的不是一个文本框,而是两个文本框。我做了调试也意识到它被触发了两次。无法弄清楚为什么它会被触发两次。这是我的应用程序的一些代码
private void TextBox_KeyDown_1(object sender, Windows.UI.Xaml.Input.KeyRoutedEventArgs e)
{
if (e.Key == Windows.System.VirtualKey.Enter)
{
//code for producing textbox
}
}
即时调试时,一旦执行上述块,它将转到 LayoutAwarePage.cs 并将控件发送到该代码段
private void CoreDispatcher_AcceleratorKeyActivated(CoreDispatcher sender,
AcceleratorKeyEventArgs args)
{
var virtualKey = args.VirtualKey;
if ((args.EventType == CoreAcceleratorKeyEventType.SystemKeyDown ||
args.EventType == CoreAcceleratorKeyEventType.KeyDown) &&
(virtualKey == VirtualKey.Left || virtualKey == VirtualKey.Right ||
(int)virtualKey == 166 || (int)virtualKey == 167))
{
var coreWindow = Window.Current.CoreWindow;
var downState = CoreVirtualKeyStates.Down;
bool menuKey = (coreWindow.GetKeyState(VirtualKey.Menu) & downState) == downState;
bool controlKey = (coreWindow.GetKeyState(VirtualKey.Control) & downState) == downState;
bool shiftKey = (coreWindow.GetKeyState(VirtualKey.Shift) & downState) == downState;
bool noModifiers = !menuKey && !controlKey && !shiftKey;
bool onlyAlt = menuKey && !controlKey && !shiftKey;
if (((int)virtualKey == 166 && noModifiers) ||
(virtualKey == VirtualKey.Left && onlyAlt))
{
// When the previous key or Alt+Left are pressed navigate back
args.Handled = true;
this.GoBack(this, new RoutedEventArgs());
}
else if (((int)virtualKey == 167 && noModifiers) ||
(virtualKey == VirtualKey.Right && onlyAlt))
{
// When the next key or Alt+Right are pressed navigate forward
args.Handled = true;
this.GoForward(this, new RoutedEventArgs());
}
}
}
完成此代码块后,控件将发送回函数 TextBox_KeyDown_1。我无法理解为什么首先将控件发送到 layoutawarepage.cs。此代码是在我的项目中添加 SplitPage 时生成的。