Windows 8.1继续关注TextBox

时间:2016-11-21 10:30:33

标签: xaml windows-store-apps winrt-xaml

对于Windows应用商店应用,需求说明:"在加载页面时以及在用户与其他组件交互后,将焦点放在TextBox上#34;。

我在加载页面时以及当用户与同一网格中的其他组件(即按钮)进行交互时解决了问题。

 MyTextBox.LostFocus += (s,e)=> {
     Dispatcher.RunAsync(
            CoreDispatcherPriority.Normal, () => SearchBox.Focus(FocusState.Programmatic));
 }

问题在于当用户与不同视图中的组件交互时,如AppBar。

也许我可以解决比较父视图的问题并运行Focus(FocusState.Programmatic),如果它们来自同一视图。

但是......怎么样?

1 个答案:

答案 0 :(得分:0)

根据您的描述,您希望将注意力集中在TextBox上。我做了一个简单的演示,你可以参考这个。

<强> XAML:

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <StackPanel HorizontalAlignment="Center" VerticalAlignment="Bottom">
    <TextBox x:Name="textbox1" Width="300" Height="50" HorizontalAlignment="Left" VerticalAlignment="Bottom"></TextBox>
    </StackPanel>
    <CommandBar>
        <AppBarToggleButton Icon="Shuffle" Label="Shuffle" Click="AppBarButton_Click" />
        <AppBarToggleButton Icon="RepeatAll" Label="Repeat" Click="AppBarButton_Click"/>
        <AppBarSeparator/>
        <AppBarButton Icon="Back" Label="Back" Click="AppBarButton_Click"/>
        <AppBarButton Icon="Stop" Label="Stop" Click="AppBarButton_Click"/>
        <AppBarButton Icon="Play" Label="Play" Click="AppBarButton_Click"/>
        <AppBarButton Icon="Forward" Label="Forward" Click="AppBarButton_Click"/>
        <CommandBar.SecondaryCommands>
            <AppBarButton Icon="Like" Label="Like" Click="AppBarButton_Click"/>
            <AppBarButton Icon="Dislike" Label="Dislike" Click="AppBarButton_Click"/>
        </CommandBar.SecondaryCommands>
        <CommandBar.Content>
            <TextBlock Text="Now playing..." Margin="12,14"/>
        </CommandBar.Content>
    </CommandBar>
</Grid>

代码背后:

        public MainPage()
    {
        this.InitializeComponent();
        this.LayoutUpdated += MainPage_LayoutUpdated;
    }

    private void MainPage_LayoutUpdated(object sender, object e)
    {
        textbox1.Focus(Windows.UI.Xaml.FocusState.Programmatic);
    }

我也找到了你可以参考的one threads