In WP7 TextBox.Focus() does not work when WebBrowser control is present on page
I need to set focus on the textbox. The problem is when a WebBrowser control is present on the page, the SIP i开发者_如何学运维s displayed as if textbox is selected, but cursor is not visible in textbox and the input does not go to the textbox.
If I comment the WebBrowser control out, then the behavior is as expected - cursor is blinking in the TextBox when page is loaded.
Here is the XAML:
<phone:PhoneApplicationPage
x:Class="WP7Sample.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768"
Loaded="MainPageLoaded">
<StackPanel x:Name="LayoutRoot">
<TextBox x:Name="txt"/>
<phone:WebBrowser/>
</StackPanel>
</phone:PhoneApplicationPage>
And the codebehind:
void MainPageLoaded(object sender, RoutedEventArgs e)
{
txt.Focus();
}
I have tried the different workarounds, but no luck. Namely I have tried to call SetFocus from the Load, NavigatedTo etc events. I have also tried to set focus to some other control and then back to the textbox, no luck either.
Could someone provide a workaround for this problem?
BTW, the problem is reproduced on the emulator, on HTC Mozart and Trophy devices all with NoDo update installed.
step 1 : Bind loaded event with respective textbox where u want to set focus
<StackPanel x:Name="ContentPanel" Margin="2,0,2,0">
<TextBox x:Name="SearchTextBox" Height="90" VerticalAlignment="Top"
Loaded="SearchTextBox_Loaded"
KeyDown="SearchTextBox_KeyDown"/>
</StackPanel>
step- 2: Now set Focus in when this event occurs
private void SearchTextBox_Loaded(object sender, RoutedEventArgs e)
{
(sender as TextBox).Focus();
}
Try using the txt.Focus() call twice. I found this looking for a solution on how to set focus on a ListBox. I ended up trying to call the Control.Focus() function twice to set the focus (firing 3 GotFocus events) and it seemed to work.
This is a dirty workaround, but you could do this. On startup have the WebBrowser
component absent from the page. Then, have the TextBox
control wired to a LostFocus
event. Something like this:
txt.LostFocus += new RoutedEventHandler(txt_LostFocus);
When it loses focus, you can safely add a WebBrowser control to the page:
void txt_LostFocus(object sender, RoutedEventArgs e)
{
LayoutRoot.Children.Add(new WebBrowser());
}
This won't let you re-focus programmaticlly later, as the WebBrowser
will be inhibiting it, but it is a way to do it at startup.
Try handling something in the GotFocus event. Maybe txt.SelectionStart = txt.Text.Length;
It works for me.
As we have provided a hack for the case for pre-Mango versions of WP7, I had not checked the scenario beforehand. The good news, guys!
In WP7 Mango the problem does not exist!
精彩评论