Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/silverlight/4.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
Silverlight 确定在文本框中按下Enter键_Silverlight_Xaml_Windows Phone 7 - Fatal编程技术网

Silverlight 确定在文本框中按下Enter键

Silverlight 确定在文本框中按下Enter键,silverlight,xaml,windows-phone-7,Silverlight,Xaml,Windows Phone 7,考虑WinPhone7中的XAML文本框 <TextBox x:Name="UserNumber" /> 这里的目标是,当用户按下屏幕键盘上的Enter按钮时,将启动一些逻辑来刷新屏幕上的内容 我想专门为Enter引发一个事件。这可能吗 事件是特定于文本框的,还是系统键盘事件 是否需要检查每个按键上的Enter?i、 e.一些模拟到ASCII 13 编写此需求的最佳方式是什么 在文本框中,这一点的直接方法是 private void textBox1_KeyDown

考虑WinPhone7中的XAML文本框

  <TextBox x:Name="UserNumber"   />

这里的目标是,当用户按下屏幕键盘上的
Enter
按钮时,将启动一些逻辑来刷新屏幕上的内容

我想专门为
Enter
引发一个事件。这可能吗

  • 事件是特定于文本框的,还是系统键盘事件
  • 是否需要检查每个按键上的
    Enter
    ?i、 e.一些模拟到ASCII 13
  • 编写此需求的最佳方式是什么

在文本框中,这一点的直接方法是

private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
    if (e.Key == Key.Enter)
    {
        Debug.WriteLine("Enter");
    }
}

您需要实现特定于该文本框的KeyDown事件,并检查KeyEventArgs以查找实际按下的键(如果它与Key.Enter匹配,则执行一些操作)


私有无效框\u KeyDown(对象发送方,KeyEventArgs e)
{
如果(e.Key.Equals(Key.Enter))
{
//做点什么
}
}
请注意,在WP7 emulator的Beta版中,尽管使用软件屏幕键盘可以正确地检测到Enter键,但如果您使用的是硬件键盘(通过按Pause/Break激活),则Enter键似乎作为键出现。未知-或者至少是在我的计算机上这样做…

禁用键盘 面临着同样的问题;上面的例子只提供了关于 如何捕获键盘按下事件(回答问题),但 禁用键盘,单击或输入,我只是将焦点设置为另一个 完全控制

这将导致禁用键盘

private void txtCodeText_KeyDown(object sender, KeyEventArgs e)
{
    if(e.Key.Equals(Key.Enter))
    {
        //setting the focus to different control
        btnTransmit.Focus();
    }
}
    private void textBox1_KeyUp(object sender, KeyEventArgs e) {
        var isEnterKey =
            e.Key == System.Windows.Input.Key.Enter ||
            e.PlatformKeyCode == 10;

        if (isEnterKey) {
            // ...
        }
    }

如果您正在使用emulator,还可以执行类似的操作,从物理键盘检测enter键

private void txtCodeText_KeyDown(object sender, KeyEventArgs e)
{
    if(e.Key.Equals(Key.Enter))
    {
        //setting the focus to different control
        btnTransmit.Focus();
    }
}
    private void textBox1_KeyUp(object sender, KeyEventArgs e) {
        var isEnterKey =
            e.Key == System.Windows.Input.Key.Enter ||
            e.PlatformKeyCode == 10;

        if (isEnterKey) {
            // ...
        }
    }

如果您不想在XAML的代码隐藏文件中添加任何代码,也不想从MVVM体系结构的角度保持设计的整洁,那么可以使用以下方法。在XAML中,在绑定中定义命令,如下所示:

 <TextBox 
Text="{Binding Text}" 
custom:KeyUp.Command="{Binding Path=DataContext.DoCommand, ElementName=root}" />

其中KeyUp类:

使用System.Windows; 使用System.Windows.Controls; 使用System.Windows.Input; 命名空间PhoneGuitarTab.Controls { 公共静态类键控 { 私有静态只读DependencyProperty KeyUpCommandBehaviorProperty=DependencyProperty.RegisterAttached( “KeyUpCommandBehavior”, 类型(TextBoxCommandBehavior), 类型(键控), 无效); /// ///在KeyUp事件上执行的命令。 /// 公共静态只读DependencyProperty CommandProperty=DependencyProperty.RegisterAttached( “命令”, 类型(ICommand), 类型(键控), 新属性元数据(OnSetCommandCallback)); /// ///要在命令执行时提供的命令参数。 /// 公共静态只读DependencyProperty命令ParameterPerProperty=DependencyProperty.RegisterAttached( “命令参数”, 类型(对象), 类型(键控), 新属性元数据(OnSetCommandParameterCallback)); /// ///设置要在KeyUp事件上执行的。 /// ///要附加命令的文本框依赖项对象 ///附加命令 [System.Diagnostics.CodeAnalysis.SuppressMessage(“Microsoft.Design”,“CA1011:ConsiderPassingBaseTypesParameters”,justify=“仅适用于按钮库”)] 公共静态void SetCommand(TextBox TextBox、ICommand命令) { SetValue(CommandProperty,command); } /// ///检索附加到的。 /// ///包含命令依赖项属性的文本框 ///附加的命令的值 [System.Diagnostics.CodeAnalysis.SuppressMessage(“Microsoft.Design”,“CA1011:ConsiderPassingBaseTypesParameters”,justify=“仅适用于按钮库”)] 公共静态ICommand GetCommand(文本框文本框) { 将textBox.GetValue(CommandProperty)作为ICommand返回; } /// ///设置所提供对象上CommandParameter attached属性的值。 /// ///用于附加CommandParameter的文本框 ///要附加的参数值 [System.Diagnostics.CodeAnalysis.SuppressMessage(“Microsoft.Design”,“CA1011:ConsiderPassingBaseTypesParameters”,justify=“仅适用于按钮库”)] 公共静态void SetCommandParameter(TextBox TextBox,object参数) { SetValue(CommandParameterProperty,参数); } /// ///获取所提供对象上CommandParameter attached属性中的值 /// ///包含CommandParameter的文本框 ///财产的价值 [System.Diagnostics.CodeAnalysis.SuppressMessage(“Microsoft.Design”,“CA1011:ConsiderPassingBaseTypesParameters”,justify=“仅适用于按钮库”)] 公共静态对象GetCommandParameter(文本框文本框) { 返回textBox.GetValue(CommandParameterProperty); } 私有静态void OnSetCommandCallback(DependencyObject DependencyObject,DependencyPropertyChangedEventArgs e) { TextBox TextBox=作为TextBox的dependencyObject; 如果(文本框!=null) { TextBoxCommandBehavior=GetOrCreateBehavior(textBox); behavior.Command=e.NewValue作为ICommand; } } 私有静态void OnSetCommandParameterCallback(DependencyObject DependencyObject,DependencyPropertyChangedEventArgs e) { TextBox TextBox=作为TextBox的dependencyObject; 如果(文本框!=null) { TextBoxCommandBehavior=GetOrCreateBehavior(textBox); behavior.CommandParameter=e.NewValue; } } 普里瓦特 using System; using System.Windows.Controls; using System.Windows.Input; namespace PhoneGuitarTab.Controls { public class TextBoxCommandBehavior : CommandBehaviorBase { public TextBoxCommandBehavior(TextBox textBoxObject) : base(textBoxObject) { textBoxObject.KeyUp += (s, e) => { string input = (s as TextBox).Text; //TODO validate user input here **//ENTER IS PRESSED!** if ((e.Key == Key.Enter) && (!String.IsNullOrEmpty(input))) { this.CommandParameter = input; ExecuteCommand(); } }; } } } using System; using System.Windows.Controls; using System.Windows.Input; namespace PhoneGuitarTab.Controls { /// /// Base behavior to handle connecting a to a Command. /// /// The target object must derive from Control /// /// CommandBehaviorBase can be used to provide new behaviors similar to . /// public class CommandBehaviorBase where T : Control { private ICommand command; private object commandParameter; private readonly WeakReference targetObject; private readonly EventHandler commandCanExecuteChangedHandler; /// /// Constructor specifying the target object. /// /// The target object the behavior is attached to. public CommandBehaviorBase(T targetObject) { this.targetObject = new WeakReference(targetObject); this.commandCanExecuteChangedHandler = new EventHandler(this.CommandCanExecuteChanged); } /// /// Corresponding command to be execute and monitored for /// public ICommand Command { get { return command; } set { if (this.command != null) { this.command.CanExecuteChanged -= this.commandCanExecuteChangedHandler; } this.command = value; if (this.command != null) { this.command.CanExecuteChanged += this.commandCanExecuteChangedHandler; UpdateEnabledState(); } } } /// /// The parameter to supply the command during execution /// public object CommandParameter { get { return this.commandParameter; } set { if (this.commandParameter != value) { this.commandParameter = value; this.UpdateEnabledState(); } } } /// /// Object to which this behavior is attached. /// protected T TargetObject { get { return targetObject.Target as T; } } /// /// Updates the target object's IsEnabled property based on the commands ability to execute. /// protected virtual void UpdateEnabledState() { if (TargetObject == null) { this.Command = null; this.CommandParameter = null; } else if (this.Command != null) { TargetObject.IsEnabled = this.Command.CanExecute(this.CommandParameter); } } private void CommandCanExecuteChanged(object sender, EventArgs e) { this.UpdateEnabledState(); } /// /// Executes the command, if it's set, providing the /// protected virtual void ExecuteCommand() { if (this.Command != null) { this.Command.Execute(this.CommandParameter); } } } }