【问题标题】:How do I focus a TextBox in an AppBar in a C# Metro application?如何在 C# Metro 应用程序的 AppBar 中聚焦 TextBox?
【发布时间】:2012-04-03 07:03:44
【问题描述】:

我有以下代码:

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

【问题讨论】:

    标签: c# windows-runtime focus winrt-xaml appbar


    【解决方案1】:

    这对我有用:

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

    【讨论】:

    • 好的,添加Loaded 有效。但是为什么它不响应其他事件呢?我认为它只会在Opened 中工作,或者我是否需要在幻灯片动画完成之前将其发送到那里? :S
    • 我认为打开的事件发生在您将 IsOpened 设置为 true 但在控件加载(添加到逻辑树)之前以及创建/添加 SearchBar 之前,因此 SearchBar 不存在并且你得到一个调试器没有捕获的 NullReferenceException - 可能是因为应用程序/框架在其他地方捕获了它。当您执行 Ctrl+Alt+E 或 Menu/Debug/Exceptions 以查看抛出的异常时,您可以在抛出的异常上启用断点。
    • 但是为什么当我关闭并打开 AppBar 时它不起作用?我知道它在第一次为空,这就是if (... = null) 存在的原因。不过下次应该不会是null了吧?
    • 我不知道 - 也许它不喜欢 FocusState.Keyboard... 似乎是在您触发焦点更改时不支持指定的内容,并且仅在您获得焦点时报告更改通知。
    猜你喜欢
    • 1970-01-01
    • 2012-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-21
    • 2013-04-07
    • 2018-02-20
    相关资源
    最近更新 更多