【问题标题】:Setting initial control focus in Silverlight在 Silverlight 中设置初始控制焦点
【发布时间】:2009-07-14 16:08:06
【问题描述】:

我正在寻找一种将 Silverlight UserControl 上的初始焦点自动设置为特定控件的方法。我有一个带有用户名文本框的登录页面,我想拥有它,这样一旦用户进入该页面,他们的光标就已经定位并在用户名文本框中等待,而不必让他们单击该框。

我尝试在 UserControl 的 Loaded 事件中调用 .Focus,但没有成功。有人知道怎么做吗?

【问题讨论】:

    标签: .net silverlight xaml silverlight-2.0


    【解决方案1】:
    public MainPage()
    {
        InitializeComponent();
    
        this.Loaded += new RoutedEventHandler(MainPage_Loaded);
    }
    
    void MainPage_Loaded(object sender, RoutedEventArgs e)
    {
        HtmlPage.Plugin.Focus();
        MyTextBox.Focus();
    }
    

    【讨论】:

      【解决方案2】:

      我创建了一个快速的 SL3 应用程序,很难将初始焦点转到 UserControl,更不用说转到 Silverlight 控件中的控件了。

      但是,看看this solution 是否为您解决了这个问题。您必须使用一点 JavaScript。

      下面是代码供参考:

      <%@ Page Language="C#" AutoEventWireup="true" %>
      
      <%@ Register Assembly="System.Web.Silverlight" Namespace="System.Web.UI.SilverlightControls"
          TagPrefix="asp" %>
      
      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
      
      <html xmlns="http://www.w3.org/1999/xhtml" style="height:100%;">
      <head runat="server">
          <title>Test Page For TextFocusTest</title>
          <script type="text/javascript">
          window.onload = function()
              {
                  document.getElementById('Xaml1').focus();
              }
          </script>
      </head>
      <body style="height:100%;margin:0;">
          <form id="form1" runat="server" style="height:100%;">
              <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
              <div  style="height:100%;">
                  <asp:Silverlight ID="Xaml1" runat="server" Source="~/ClientBin/TextFocusTest.xap" Version="2.0" Width="100%" Height="100%" />
              </div>
          </form>
      </body>
      </html>
      

      一旦您的 SL 控件获得焦点,您可以使用以下方式进一步将焦点设置到特定控件:

      namespace SilverlightApplication2
      {
          public partial class MainPage : UserControl
          {
              public MainPage ()
              {
                  InitializeComponent ();
      
                  this.GotFocus += new RoutedEventHandler (MainPage_GotFocus);
              }
      
              void MainPage_GotFocus (object sender, RoutedEventArgs e)
              {
                  uxLogin.Focus ();
              }
          }
      }
      

      其中 uxLogin 在 XAML 中定义为:

      <TextBox x:Name="uxLogin" Height="25" Width="100" />
      

      【讨论】:

      • 此解决方案似乎在 IE 中有效,但我无法让 Silverlight 控件在 Firefox 或 Chrome 中获得焦点。
      • 仍然无法在其他浏览器中使用此功能,但我将使用此解决方案,因为大多数用户将使用 IE,而其他用户只需单击 Silverlight 控件。谢谢!
      • 欢迎您。我在使用 Silverlight 和 FF 时遇到了很多问题——调整大小并让控件显示。在一种情况下,我最终放弃了,为 IE 提供了一种解决方案,为 FF 提供了另一种解决方案。不是最好的方法,但很好。
      【解决方案3】:

      如果您想以 PRISM 或 MVVM 方式执行此操作(摆脱代码隐藏代码),您可以实现一个行为。在我的情况下,如果用户名字段为空,我将视图集中在用户名字段中,如果已设置,则将其集中在密码中(因此有 2 个参数)。

      我的实现:

      public static class ControlTextFocusBehavior
      {
          public static readonly DependencyProperty FocusParameterProperty = DependencyProperty.RegisterAttached(
            "FocusParameter",
            typeof(string),
            typeof(ControlTextFocusBehavior),
            new PropertyMetadata(OnSetFocusParameterCallBack));
      
          public static readonly DependencyProperty IsEmptyFocusedProperty = DependencyProperty.RegisterAttached(
            "IsEmptyFocused",
            typeof(bool),
            typeof(ControlTextFocusBehavior),
            new PropertyMetadata(true));
      
      
          private static void OnSetFocusParameterCallBack(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
          {
              Control control = dependencyObject as Control;
              if (control != null)
              {
                  control.Loaded += new RoutedEventHandler(control_Loaded);
              }
          }
      
          private static void control_Loaded(object sender, RoutedEventArgs e)
          {
              Control control = sender as Control;
              control.Loaded -= new RoutedEventHandler(control_Loaded);
      
              DependencyObject dependencyObject = sender as DependencyObject;
              if (dependencyObject != null)
              {
                  bool isEmptyFocused = GetIsEmptyFocused(dependencyObject);
                  bool isNullOrEmpty = string.IsNullOrEmpty(GetFocusParameter(dependencyObject));
                  if ((isEmptyFocused && isNullOrEmpty) ||
                      (!isEmptyFocused && !isNullOrEmpty))
                  {
                      HtmlPage.Plugin.Focus();
                      control.Focus();
                  }
              }
          }
      
          public static void SetFocusParameter(DependencyObject dependencyObject, string parameter)
          {
              dependencyObject.SetValue(FocusParameterProperty, parameter);
          }
      
          public static string GetFocusParameter(DependencyObject dependencyObject)
          {
              return dependencyObject.GetValue(FocusParameterProperty) as string;
          }
      
      
          public static void SetIsEmptyFocused(DependencyObject dependencyObject, bool parameter)
          {
              dependencyObject.SetValue(IsEmptyFocusedProperty, parameter);
          }
      
          public static bool GetIsEmptyFocused(DependencyObject dependencyObject)
          {
              return (bool)dependencyObject.GetValue(IsEmptyFocusedProperty);
          }
      }
      

      【讨论】:

        【解决方案4】:

        如果你想要这个代码:

        window.onload = 函数()

            {
                document.getElementById('Xaml1').focus();
            }
        

        适用于所有浏览器,您必须为 id="Xaml1" 的元素设置 tabIndex。

        对不起我的英语:)

        【讨论】:

          【解决方案5】:

          我刚刚做了一个快速测试,看起来这也解决了浏览器中的问题!

          How to set focus on TextBox in Silverlight 4 out-of-browser popup

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2011-05-27
            • 2014-12-19
            • 1970-01-01
            • 2011-07-04
            • 1970-01-01
            • 2011-02-14
            • 2013-03-24
            • 2010-09-18
            相关资源
            最近更新 更多