Silverlight 登录自动焦点等页面的小细节【自动设置焦点,回车登陆】 .

来源:互联网 发布:arkaos grand vj mac 编辑:程序博客网 时间:2024/04/30 17:29
 

作为登陆页面,页面打开时焦点停在用户名的文本框,以及回车键可登陆是比较方便的,但是silverlight默认却没有在页面打开时将焦点设在指定的文本框,也不支持默认按钮。

这需要我们作一点工作。

1.自动设置焦点

开始我尝试过将tabindex设为0,当前页设置焦点[this.Focus()],指定控件设置焦点[this.TextBox1.Focus()],都无法实现在页面打开时将焦点停在指定的控件上。

其实我是忘了一件事,silverlight程序是作为一个插件嵌入在aspx页面中,所谓的页面打开是aspx的页面打开,此时的焦点是停在aspx页面上,而不是silverlight插件上,所以无论你在silverlight程序中怎么Focus都是取不到焦点的。

要实现这个前提条件是在aspx页面打开时【sl插件loaded时】将页面的焦点停在silverlight插件上,然后再在silverlight程序中设置控件的焦点,具体代码如下:

1void Login_Loaded(object sender, RoutedEventArgs e)
2
{
3//用户名文本框自动聚焦

4 HtmlPage.Plugin.Focus();//silverlight插件聚焦
5this.tbxUserName.Focus();//用户名文本聚焦
6 }

2.回车登陆

这不是一个最好的办法,但是也可以实现回车登陆,大家可以参考一下

具体思路是捕捉页面的keydown事件,然后触发登陆按钮的点击事件,具体代码如下:

///<summary>
/// 页面键盘事件处理
///</summary>

void Login_KeyDown(object sender, KeyEventArgs e)
{
//回车键登陆

if (e.Key== Key.Enter)
{
btnOK_Click(sender,
new
RoutedEventArgs());//登陆按钮的点击事件处理方法
}
}
===================================
键盘控制:

Microsoft Silverlight provides a set of keyboard events that enable you to respond to keystroke actions.

This topic contains the following sections:

  • Keyboard Events
  • Defining a Keyboard Event Handler
  • Platform Differences for Keyboard Events
  • Inputting Text

Keyboard Events

Silverlight provides the keyboard events described in the following table.

EventDescriptionKeyDownOccurs when a key is pressed while the plug-in has focus.KeyUpOccurs when a key is released while the plug-in has focus.

 

Note   Handling keyboard events might vary between browsers. When you create an application that uses keyboard input, make sure to test the application in your target browsers.

The following XAML example shows how to define theKeyDown event for the Canvas object. Notice that the event-handler function cannot be called with parameter values, unlike JavaScript event-handler functions.

XAML
<Canvas  xmlns="http://schemas.microsoft.com/client/2007"  KeyUp="onKeyUp" />

Note   Keyboard events can be defined only for the rootCanvas object of a Silverlight plug-in.

The following example shows how to define a Silverlight keyboard event in JavaScript. If you attempt to define a keyboard event for any object other than the rootCanvas object, a parser exception is thrown.

JavaScript
function onLoaded(sender, eventArgs){    // Set the root Canvas object to a KeyUp event handler function.    sender.addEventListener("KeyUp", onKeyUp);}

Keyboard Events and Focus

In order for a keyboard event to be received, the Silverlight plug-in needs to have focus; otherwise, the events are not generated. A Silverlight plug-in can gain focus through user actions such as clicking the plug-in or tabbing to it.

Keyboard Events and the Browser

The browser determines which keystrokes it interprets as commands, and which keystrokes it passes on to hosted content. This means that certain keystrokes cannot be retrieved fromKeyDown and KeyUp event-handler functions. Most keystrokes that a browser interprets as commands are shortcut, or accelerator, keystokes. For example,CTRL+D is a shortcut keystroke combination for adding a favorite URL to the Firefox and Internet Explorer browsers.

Keyboard Events and Full-Screen Mode

When a Silverlight plug-in is displayed in full-screen mode, keyboard events are prevented from being passed to keyboard event handlers in the application. The only valid keyboard input that is acted upon is the set of keystrokes that returns the Silverlight plug-in to embedded mode. This limitation of keyboard input during full-screen mode is a security feature. It is intended to minimize the possibility of unintended information being entered by a user. For more information on full-screen mode, see Silverlight Full-Screen Support.

Defining a Keyboard Event Handler

A Silverlight keyboard event-handler function contains two parameters, as described in the following table.
ParameterDescriptionsenderIdentifies the Silverlight object that generated the event. You can retrieve the type value of the object by calling theToString method on the parameter value.keyEventArgsIdentifies the set of argument values for the specific event. ThekeyEventArgs parameter contains the following values:
  • Key -- An integer value that represents the key that is down. This value is the portable key code, which is not operating system-specific.
  • PlatformKeyCode -- An integer value that represents the key that is down. This value is the non-portable key code, which is operating system-specific.
  • Shift -- A Boolean value that determines whether theSHIFT key is down.
  • Ctrl -- A Boolean value that determines whether theCTRL key is down.

Note   If the Silverlight event-handler function does not reference thesender and keyEventArgs parameters, you do not have to define them as part of the function declaration.

The following JavaScript example shows how to define a Silverlight event-handler function for theKeyDown event. In this case, the code displays a string in an alert dialog box that contains the version of the Silverlight plug-in.

JavaScript
function onKeyUp(sender, keyEventArgs){    // Determine whether the keystroke combination CTRL+V was detected.    if ((keyEventArgs.key == 51) && (keyEventArgs.ctrl == true))    {        // Retrieve a reference to the plug-in.        var plugin = sender.getHost();        // Determine whether the 1.0 version of Silverlight is available.        alert("Silverlight 1.0: " + plugin.isVersionSupported("1.0"));    }}

Detecting the SHIFT and CTRL Keys in a Mouse Event Handler

The MouseEnter,MouseLeftButtonDown,MouseLeftButtonUp, andMouseMove events enable you to determine whether theSHIFT or CTRL keys are down when the mouse event-handling function is invoked. The following JavaScript example shows how to implement aMouseLeftButtonDown event-handler function that displays the values of themouseEventArgs parameter.

JavaScript
function onMouseLeftButtonUp(sender, mouseEventArgs){    // Concatenate the values of the MouseEventArgs parameter.    var msg  = "x = " + mouseEventArgs.getPosition(null).x;    msg += "  y = " + mouseEventArgs.getPosition(null).y;    msg += "  shift = " + mouseEventArgs.shift;    msg += "  ctrl = " + mouseEventArgs.ctrl;    alert(msg);}

Platform Differences for Keyboard Events

The keyboardEventArgs parameter of KeyDown and KeyUp events provides two types of key codes:
  • Key --A portable key code that is not operating system-specific.
  • PlatformKeyCode --A non-portable key code that is operating system-specific.

The portable key codes are a common subset of all the possible key codes of the supported operating systems, in this case, Macintosh and Windows. For example, the keystroke 'v' is represented as aKey value of 51, and a PlatformKeyCode value of 86. Certain keystrokes, however, are not portable, such as the WindowsSCROLL LOCK key. In this case, the Key value is 255, which is the value for an unknown key, and thePlatformKeyCode is 145 on a Windows platform. For information about Windows-specific key codes, see "Virtual-Key Codes" in theMSDN Library. For information on Macintosh-specific key codes, seeKeyboard Layout Services Reference on the Apple Developer Connection Web site.

Detecting Platforms in JavaScript

When you visit a Web page, your browser sends the user agent string to the server that is hosting the site you are visiting. This string indicates which browser you are using, its version number, and details about your system, such as operating system and version. You can use this string to determine the platform the browser-hosted Silverlight plug-in is running on. The following JavaScript example shows how to create a set of variables that you can use in keyboard event-handling functions to determine the platform.

JavaScript
// Create variables to detect browser platform.var is_mac = (navigator.userAgent.indexOf("Macintosh") != -1);var is_win = (navigator.userAgent.indexOf("Windows") != -1);

Inputting Text

There is no "TextBox" control for Silverlight 1.0. To input text into a Silverlight-based application, you can overlay an HTML control such asTEXTAREA over your Silverlight-based application. You would then use JavaScript to pass the inputted text back and forth between the Silverlight-based application and the HTMLTEXTAREA control. See Mixing Object Models for an example. Also, seeUsing Input Method Editors for Text Entry in Silverlight for a video that shows how to allow user text input.

========================
//听app的KEY_DOWN事件
this.addEventListener(KeyboardEvent.KEY_DOWN, onEnter);
private function onEnter(event:KeyboardEvent):void
{
if(event.keyCode == Keyboard.ENTER && focusManager.getFocus() is TextInput)
{
focusManager.setFocus(focusManager.getNextFocusManagerComponent());
}
}