我想使用C#/ XAML在Windows 8 UI / Metro UI应用中的文本框上实现自动完成。
此时,当软/触摸键盘显示时,它会遮挡自动完成框。但是,在文本框焦点上,Windows 8会自动向上滚动整个视图并确保文本框处于焦点。
实际上,我想要的是向上滚动一点的视图(事实上,通过自动完成框的高度)。
我意识到我可以拦截InputPane.GetForCurrentView()
的Showing事件我可以在显示事件中将InputPaneVisibilityEventArgs.EnsuredFocusedElementInView设置为true(因此Windows不会尝试做任何事情)....但是,我如何调用Windows 8可以执行的相同滚动功能,但是要求它滚动一点!?
以下是主页的代码:
<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
<StackPanel VerticalAlignment="Center" HorizontalAlignment="Center" Margin="0,200,0,0">
<TextBlock HorizontalAlignment="Center" FontSize="60">App 1</TextBlock>
<TextBlock HorizontalAlignment="Center">Enter text below</TextBlock>
<TextBox HorizontalAlignment="Center" Margin="-10,0,10,0" Width="400" Height="30"/>
<ListBox HorizontalAlignment="Center" Width="400">
<ListBoxItem>Auto complete item 1</ListBoxItem>
<ListBoxItem>Auto complete item 2</ListBoxItem>
<ListBoxItem>Auto complete item 3</ListBoxItem>
<ListBoxItem>Auto complete item 4</ListBoxItem>
<ListBoxItem>Auto complete item 5</ListBoxItem>
</ListBox>
</StackPanel>
</Grid>
如果启动分辨率最低的模拟器,请用手“触摸”文本框,这将打开软键盘。在真实应用程序中,当用户输入文本时,自动完成列表将显示项目。
简而言之,我如何将屏幕向上移动一点以便用户可以看到整个自动完成列表?
请记住,在真实应用中,它会更糟糕,因为用户可能甚至没有注意到键盘下面出现的自动完成列表。
我真的很感激一些建议,非常感谢!
答案 0 :(得分:4)
我为Windows应用商店应用创建了一个AutoCompleteBox,nuget包可以在https://nuget.org/packages/AutoCompleteBoxWinRT
获得答案 1 :(得分:0)
好的,这就是我要解决的问题,因为我似乎无法根据键盘的外观找到控制应用程序滚动的方法。我将创建一个用户控件,它将构成自动完成文本框的基础。
<UserControl
x:Class="App6.MyUserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App6"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300"
d:DesignWidth="400">
<Grid>
<TextBox x:Name="textBox" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" GotFocus="textBox_GotFocus" LostFocus="textBox_LostFocus" />
<ListBox x:Name="listBox" Height="150" Margin="0,-150,0,0" VerticalAlignment="Top" Visibility="Collapsed"/>
</Grid>
这是一个非常基本的实现,所以你必须调整以满足你的需求。
然后,我将以下代码隐藏添加到用户控件
public sealed partial class MyUserControl1 : UserControl
{
// Rect occludedRect;
bool hasFocus = false;
public MyUserControl1()
{
this.InitializeComponent();
InputPane.GetForCurrentView().Showing += MyUserControl1_Showing;
}
void MyUserControl1_Showing(InputPane sender, InputPaneVisibilityEventArgs args)
{
if (hasFocus)
{
var occludedRect = args.OccludedRect;
var element = textBox.TransformToVisual(null);
var point = element.TransformPoint(new Point(0, 0));
if (occludedRect.Top < point.Y + textBox.ActualHeight + listBox.ActualHeight)
{
listBox.Margin = new Thickness(0, -listBox.ActualHeight, 0, 0); // Draw above
}
else
{
listBox.Margin = new Thickness(0, textBox.ActualHeight, 0, 0); // draw below
}
}
}
private void textBox_GotFocus(object sender, RoutedEventArgs e)
{
listBox.Visibility = Windows.UI.Xaml.Visibility.Visible;
hasFocus = true;
}
private void textBox_LostFocus(object sender, RoutedEventArgs e)
{
listBox.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
hasFocus = false;
}
}
接下来的步骤是公开属性以传递要绑定到ListBox的数据。硬核将是ListBoxItem模板等等,具体取决于您希望它的可重用性。