2016-11-21 26 views
1

对于Windows应用商店应用,需求说:“在页面加载时以及用户与其他组件进行交互后,将重点放在文本框上”。Windows 8.1重点关注文本框

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

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

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

也许我可以解决比较父视图和运行焦点(FocusState.Programmatic)如果他们来自同一视图的问题。

但是...怎么样?

回答

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