簡體   English   中英

如何在C#Metro應用程序的AppBar中聚焦文本框?

[英]How do I focus a TextBox in an AppBar in a C# Metro application?

我有以下代碼:

<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嗎?

這對我有用:

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);
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM