2016-11-21 28 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你可以參考一下。