【问题标题】:Windows 8.1 Keep focus on TextBoxWindows 8.1 继续关注 TextBox
【发布时间】:2016-11-21 10:30:32
【问题描述】:

对于 Windows 应用商店应用程序,要求是:“在加载页面时以及在用户与其他组件交互后,将焦点放在 TextBox 上”。

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

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

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

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

但是……怎么办?

【问题讨论】:

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


    【解决方案1】:

    根据您的描述,您希望继续关注 TextBox。我做了一个简单的demo,大家可以参考一下。

    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你可以参考。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多