Xaml Can';t当键盘打开时滚动至结果结束(Windows Phone)
我正在开发Windows Phone应用程序,遇到以下问题: 我有一个显示搜索结果的列表控件,但是当键盘打开时,由于我的键盘,我的一些结果不可见 有没有办法将控件缩小到键盘边框?以查看所有结果 即使打开键盘,我也要滚动到结果结束。这是我的解决方案Xaml Can';t当键盘打开时滚动至结果结束(Windows Phone),xaml,layout,windows-phone-8.1,Xaml,Layout,Windows Phone 8.1,我正在开发Windows Phone应用程序,遇到以下问题: 我有一个显示搜索结果的列表控件,但是当键盘打开时,由于我的键盘,我的一些结果不可见 有没有办法将控件缩小到键盘边框?以查看所有结果 即使打开键盘,我也要滚动到结果结束。这是我的解决方案 public class ResizeContentOnKeyboardShowingBehavior : Behavior<Page> { private readonly double _screenHeight
public class ResizeContentOnKeyboardShowingBehavior : Behavior<Page>
{
private readonly double _screenHeight;
public ResizeContentOnKeyboardShowingBehavior()
{
_screenHeight = Window.Current.Bounds.Height;
}
protected override void OnAttached()
{
InputPane.GetForCurrentView().Showing += OnKeyboardShowing;
InputPane.GetForCurrentView().Hiding += OnKeyboardHiding;
}
protected override void OnDetaching()
{
InputPane.GetForCurrentView().Showing -= OnKeyboardShowing;
InputPane.GetForCurrentView().Hiding -= OnKeyboardHiding;
}
private void OnKeyboardHiding(InputPane sender, InputPaneVisibilityEventArgs args)
{
var content = (FrameworkElement)AssociatedObject.Content;
content.Height = _screenHeight;
}
private void OnKeyboardShowing(InputPane sender, InputPaneVisibilityEventArgs args)
{
var content = (FrameworkElement)AssociatedObject.Content;
double keyboardHeight = sender.OccludedRect.Height;
content.Height = _screenHeight - keyboardHeight;
}
}
这是我的解决办法
public class ResizeContentOnKeyboardShowingBehavior : Behavior<Page>
{
private readonly double _screenHeight;
public ResizeContentOnKeyboardShowingBehavior()
{
_screenHeight = Window.Current.Bounds.Height;
}
protected override void OnAttached()
{
InputPane.GetForCurrentView().Showing += OnKeyboardShowing;
InputPane.GetForCurrentView().Hiding += OnKeyboardHiding;
}
protected override void OnDetaching()
{
InputPane.GetForCurrentView().Showing -= OnKeyboardShowing;
InputPane.GetForCurrentView().Hiding -= OnKeyboardHiding;
}
private void OnKeyboardHiding(InputPane sender, InputPaneVisibilityEventArgs args)
{
var content = (FrameworkElement)AssociatedObject.Content;
content.Height = _screenHeight;
}
private void OnKeyboardShowing(InputPane sender, InputPaneVisibilityEventArgs args)
{
var content = (FrameworkElement)AssociatedObject.Content;
double keyboardHeight = sender.OccludedRect.Height;
content.Height = _screenHeight - keyboardHeight;
}
}
您的代码可以用作infra,这非常有用!:)这是一个很好的解决方案。我已经实现了它,并且调整了内容的大小,这样键盘就不会与应用程序的窗口重叠,但是内容不会滚动到视图中。我可以点击并拖动内容到视图中。知道为什么吗?找到了原因:我的内容顶部有一个空白,对应于键盘的高度(或者可能是高度的一半)。当我添加
content.VerticalAlignment=VerticalAlignment.Top
时,它解决了这个问题。所以它是关于内容的placemenet,一旦它被调整大小。也许调整父容器的大小比调整内容更好。您的代码可以用作infra,这非常有用!:)这是一个很好的解决方案。我已经实现了它,并且调整了内容的大小,这样键盘就不会与应用程序的窗口重叠,但是内容不会滚动到视图中。我可以点击并拖动内容到视图中。知道为什么吗?找到了原因:我的内容顶部有一个空白,对应于键盘的高度(或者可能是高度的一半)。当我添加content.VerticalAlignment=VerticalAlignment.Top
时,它解决了这个问题。所以它是关于内容的placemenet,一旦它被调整大小。也许调整父容器的大小比调整内容的大小更好。
<Page x:Class="MainView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:interactivity="using:Microsoft.Xaml.Interactivity"
xmlns:behaviors="using:Behaviors">
<interactivity:Interaction.Behaviors>
<behaviors:ResizeContentOnKeyboardShowingBehavior />
</interactivity:Interaction.Behaviors>
<Grid>
</Grid>
public sealed partial class MainPage : Page
{
private readonly InputPane _inputPane;
private readonly double _screenHeight;
public MainPage()
{
this.InitializeComponent();
_screenHeight = Window.Current.Bounds.Height;
_inputPane = InputPane.GetForCurrentView();
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
_inputPane.Hiding += OnKeyboardHiding;
_inputPane.Showing += OnKeyboardShowing;
}
protected override void OnNavigatedFrom(NavigationEventArgs e)
{
base.OnNavigatedFrom(e);
_inputPane.Hiding -= OnKeyboardHiding;
_inputPane.Showing -= OnKeyboardShowing;
}
private void OnKeyboardShowing(InputPane sender, InputPaneVisibilityEventArgs args)
{
var content = (FrameworkElement)Window.Current.Content;
double keyboardHeight = sender.OccludedRect.Height;
content.Height = _screenHeight - keyboardHeight;
}
private void OnKeyboardHiding(InputPane sender, InputPaneVisibilityEventArgs args)
{
var content = (FrameworkElement)Window.Current.Content;
content.Height = _screenHeight;
}
}