2012-03-18 19 views
4

我有以下代碼:如何將文本框集中在C#Metro應用程序的AppBar中?

<Page.BottomAppBar> 
    <AppBar x:Name="MyAppBar" Height="32" IsOpen="True" Opened="MyAppBar_Opened"> 
     <TextBox x:Name="SearchBar" Grid.Row="2" KeyDown="SearchBar_KeyDown"/> 
    </AppBar> 
</Page.BottomAppBar> 

的問題是應用程序打開時,沒有焦點,它也沒有焦點,當我點擊右鍵兩次,關閉並重新打開。

我現在有兩個MyAppBar_OpenedOnNavigatedTo下面的代碼:

if (MyAppBar == null) 
    return; 

MyAppBar.Focus(Windows.UI.Xaml.FocusState.Keyboard); 

if (SearchBar == null) 
    return; 

SearchBar.Focus(Windows.UI.Xaml.FocusState.Keyboard); 

但是,這似乎並沒有產生任何影響,其他枚舉值像MouseProgrammatic不工作的。我做錯了什麼導致沒有激活?

我知道以前用過的替代方案,但是Metro FocusManager沒有SetFocusedElement?

回答

2

這個工作對我來說:

XAML:

<Page 
    x:Class="Application5.BlankPage" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:local="using:Application5" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    mc:Ignorable="d"> 

    <Grid Background="{StaticResource ApplicationPageBackgroundBrush}"> 
     <AppBar 
      x:Name="MyAppBar" 
      Height="32" 
      IsOpen="True" 
      Opened="MyAppBar_Opened" 
      Loaded="MyAppBar_Loaded" 
      VerticalAlignment="Bottom"> 
      <TextBox 
       x:Name="SearchBar" 
       HorizontalAlignment="Stretch" /> 
     </AppBar> 
    </Grid> 
</Page> 

後面的代碼:

using System; 
using System.Collections.Generic; 
using System.IO; 
using System.Linq; 
using Windows.Foundation; 
using Windows.Foundation.Collections; 
using Windows.UI.Xaml; 
using Windows.UI.Xaml.Controls; 
using Windows.UI.Xaml.Controls.Primitives; 
using Windows.UI.Xaml.Data; 
using Windows.UI.Xaml.Input; 
using Windows.UI.Xaml.Media; 
using Windows.UI.Xaml.Navigation; 

// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238 

namespace Application5 
{ 
    /// <summary> 
    /// An empty page that can be used on its own or navigated to within a Frame. 
    /// </summary> 
    public sealed partial class BlankPage : Page 
    { 
     public BlankPage() 
     { 
      this.InitializeComponent(); 
     } 

     /// <summary> 
     /// Invoked when this page is about to be displayed in a Frame. 
     /// </summary> 
     /// <param name="e">Event data that describes how this page was reached. The Parameter 
     /// property is typically used to configure the page.</param> 
     protected override void OnNavigatedTo(NavigationEventArgs e) 
     { 
     } 

     private void MyAppBar_Opened(object sender, object e) 
     { 
      if (SearchBar != null) 
       SearchBar.Focus(Windows.UI.Xaml.FocusState.Programmatic); 
     } 

     private void MyAppBar_Loaded(object sender, RoutedEventArgs e) 
     { 
      if (SearchBar != null) 
       SearchBar.Focus(Windows.UI.Xaml.FocusState.Programmatic); 
     } 
    } 
} 
+0

好了,加入了'Loaded'工作。但爲什麼它不響應其他事件呢?我會認爲它會在'Opened'中工作,或者我需要在那裏調度它直到幻燈片動畫完成? :S – 2012-03-19 05:30:48

+0

我認爲打開的事件發生在設置IsOpened爲true時,但在控件加載(添加到邏輯樹)之前以及在創建/添加SearchBar之前,因此SearchBar不存在,並且您得到一個NullReferenceException調試器沒有捕獲 - 也許是因爲應用程序/框架在其他地方捕獲它。當您執行Ctrl + Alt + E或菜單/調試/例外以查看它被拋出時,您可以對引發的異常啓用斷點。 – 2012-03-19 16:15:24

+0

但是,當我關閉並打開AppBar時,爲什麼它不起作用?我知道它在第一次變爲null,這就是爲什麼'if(... = null)'在那裏。但是,下一次它不應該是'空',應該嗎? – 2012-03-19 19:13:27

相關問題