如何将TextBox集中在C#Metro应用程序的AppBar中?
发布时间:2020-12-16 07:29:28 所属栏目:百科 来源:网络整理
导读:我有以下代码: 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 问题是它在应用程序打开时没有焦点,当
我有以下代码:
<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=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); } } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- Use SQLite Instead of Local Storage In Ionic Framework
- Exception:两个类具有相同的 XML 类型名称,请使用 @XmlType
- ruby-on-rails – 使用Rails环境的“私有”Rake任务
- 关于 ST eMotion board 使用DFU的一点总结
- 在postgresql之间的两个时间戳之间计数几个月?
- swfupload 上传 问题的处理
- CLLocation distanceFromLocation(在Swift?中)
- C++访问Redis的mset 二进制数据接口封装方案
- 學習 React.js:用 Node 和 React.js 創建一個實時的 Twitt
- Winform加载二进制流的flash