在 WP7 中,当页面上存在 WebBrowser 控件时,TextBox.Focus() 不起作用

发布于 2024-11-05 23:44:37 字数 1132 浏览 1 评论 0原文

我需要将焦点设置在文本框上。问题是,当页面上存在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

若水微香 2024-11-12 23:44:37

第 1 步:将加载的事件与要设置焦点的相应文本框绑定

<StackPanel x:Name="ContentPanel" Margin="2,0,2,0">
  <TextBox x:Name="SearchTextBox" Height="90" VerticalAlignment="Top" 
           Loaded="SearchTextBox_Loaded"
           KeyDown="SearchTextBox_KeyDown"/>
</StackPanel>

第 2 步:现在在该事件发生时设置焦点

private void SearchTextBox_Loaded(object sender, RoutedEventArgs e)  
{  
    (sender as TextBox).Focus();  
}

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();  
}
猛虎独行 2024-11-12 23:44:37

尝试使用 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.

So尛奶瓶 2024-11-12 23:44:37

这是一个肮脏的解决方法,但你可以这样做。启动时,页面中不存在 WebBrowser 组件。然后,将 TextBox 控件连接到 LostFocus 事件。像这样的事情:

txt.LostFocus += new RoutedEventHandler(txt_LostFocus);

当它失去焦点时,您可以安全地向页面添加 WebBrowser 控件:

void txt_LostFocus(object sender, RoutedEventArgs e)
{
    LayoutRoot.Children.Add(new WebBrowser());
}

这不会让您稍后以编程方式重新聚焦,因为 WebBrowser 将抑制它,但它是一种在启动时执行此操作的方法。

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.

甜中书 2024-11-12 23:44:37

尝试在 GotFocus 事件中处理某些内容。也许 txt.SelectionStart = txt.Text.Length; 它对我有用。

Try handling something in the GotFocus event. Maybe txt.SelectionStart = txt.Text.Length; It works for me.

埋情葬爱 2024-11-12 23:44:37

由于我们已经为 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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文