在 WP7 中,当页面上存在 WebBrowser 控件时,TextBox.Focus() 不起作用
我需要将焦点设置在文本框上。问题是,当页面上存在 WebBrowser 控件时,SIP 会显示为就像选择了文本框一样,但光标在文本框中不可见,并且输入不会转到文本框。
如果我注释掉 WebBrowser 控件,则行为如预期 - 加载页面时光标在文本框中闪烁。
这是 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>
和代码隐藏:
void MainPageLoaded(object sender, RoutedEventArgs e)
{
txt.Focus();
}
我尝试了不同的解决方法,但没有成功。 也就是说,我尝试从 Load、NavigedTo 等事件中调用 SetFocus。我还尝试将焦点设置到其他控件,然后返回文本框,但也没有成功。
有人可以提供解决此问题的方法吗?
顺便说一句,该问题在模拟器、HTC Mozart 和 Trophy 设备上重现,所有设备均安装了 NoDo 更新。
I need to set focus on the textbox. The problem is when a WebBrowser control is present on the page, the SIP is 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
第 1 步:将加载的事件与要设置焦点的相应文本框绑定
第 2 步:现在在该事件发生时设置焦点
step 1 : Bind loaded event with respective textbox where u want to set focus
step- 2: Now set Focus in when this event occurs
尝试使用 txt.Focus() 调用两次。我发现这是在寻找有关如何将焦点设置在列表框上的解决方案。我最终尝试调用 Control.Focus() 函数两次来设置焦点(触发 3 个 GotFocus 事件),它似乎有效。
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.
这是一个肮脏的解决方法,但你可以这样做。启动时,页面中不存在
WebBrowser
组件。然后,将TextBox
控件连接到LostFocus
事件。像这样的事情:当它失去焦点时,您可以安全地向页面添加 WebBrowser 控件:
这不会让您稍后以编程方式重新聚焦,因为
WebBrowser
将抑制它,但它是一种在启动时执行此操作的方法。This is a dirty workaround, but you could do this. On startup have the
WebBrowser
component absent from the page. Then, have theTextBox
control wired to aLostFocus
event. Something like this:When it loses focus, you can safely add a WebBrowser control to the page:
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.尝试在 GotFocus 事件中处理某些内容。也许 txt.SelectionStart = txt.Text.Length; 它对我有用。
Try handling something in the GotFocus event. Maybe
txt.SelectionStart = txt.Text.Length;
It works for me.由于我们已经为 WP7 的 Mango 之前版本提供了一个 hack,所以我没有事先检查该场景。
好消息,伙计们!
在 WP7 Mango 中,这个问题不存在!
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!