如何将TextBox集中在C#Metro应用程序的AppBar中?

如何将TextBox集中在C#Metro应用程序的AppBar中?,第1张

概述我有以下代码: <Page.BottomAppBar> <AppBar x:Name="MyAppBar" Height="32" IsOpen="True" Opened="MyAppBar_Opened"> <TextBox x:Name="SearchBar" Grid.Row="2" KeyDown="SearchBar_KeyDown"/> </AppBa 我有以下代码:

<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_Opened和OnNavigatedTo中都有以下代码:

if (MyAppbar == null)    return;MyAppbar.Focus(windows.UI.Xaml.Focusstate.Keyboard);if (Searchbar == null)    return;Searchbar.Focus(windows.UI.Xaml.Focusstate.Keyboard);

但这似乎没有任何影响,其他枚举值如Mouse或Programmatic也不起作用.我做错了什么导致没有激活?

我知道之前曾经有过的替代方案,但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=234238namespace 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);        }    }}
总结

以上是内存溢出为你收集整理的如何将TextBox集中在C#Metro应用程序的AppBar中?全部内容,希望文章能够帮你解决如何将TextBox集中在C#Metro应用程序的AppBar中?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/1230010.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-06
下一篇2022-06-06

发表评论

登录后才能评论

评论列表(0条)

    保存