简体   繁体   中英

I cannot get this textbox to get focus when the application first starts

...in Silverlight. No tags in the title, and all that. I'm using Silverlight 4 on Chrome, though I've tried it on IE too. I've tried:

System.Windows.Browser.HtmlPage.Plugin.Focus();
this.Focus();
Dispatcher.BeginInvoke(() => { textBox_username.Focus(); });

And various combinations thereof (as suggested by a similar thread), as well as a separate method that was attached to the form loaded event. The above gets me the closest, since clicking anywhere on the page gives my textbox focus, like the plugin isn't getting focus still. Could be because this is the first window viewed?

I dont think that was because the first window viewed. try to put this.Focus(); after your Dispatcher you set it false;

Change your code similar to below

public partial class MainPage : UserControl

{

public MainPage()
{
    InitializeComponent();
    this.Loaded += MainPage_Loaded;
}

void MainPage_Loaded(object sender, RoutedEventArgs e)
{
    System.Windows.Browser.HtmlPage.Plugin.Focus();
    txtBox.UpdateLayout();
    txtBox.Focus();
}

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM