【发布时间】:2016-03-03 06:30:12
【问题描述】:
我正在开发 Windows Phone 应用,但遇到了这个问题: 我有一个显示搜索结果的列表控件,但是当我打开键盘时,由于我的键盘,我的一些结果不可见...
有没有办法将控件缩小到键盘边框?为了看到所有的结果。
即使打开键盘,我也想滚动到结果的末尾。
【问题讨论】:
标签: xaml layout windows-phone-8.1
我正在开发 Windows Phone 应用,但遇到了这个问题: 我有一个显示搜索结果的列表控件,但是当我打开键盘时,由于我的键盘,我的一些结果不可见...
有没有办法将控件缩小到键盘边框?为了看到所有的结果。
即使打开键盘,我也想滚动到结果的末尾。
【问题讨论】:
标签: xaml layout windows-phone-8.1
有我的解决方案
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 abstract class Behavior : DependencyObject, IBehavior
{
public DependencyObject AssociatedObject { get; set; }
public virtual void Attach(DependencyObject associatedObject)
{
AssociatedObject = associatedObject;
}
public virtual void Detach()
{
}
}
public abstract class Behavior<T> : Behavior
where T : DependencyObject
{
[System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]
public new T AssociatedObject { get; set; }
public override void Attach(DependencyObject associatedObject)
{
base.Attach(associatedObject);
this.AssociatedObject = (T)associatedObject;
OnAttached();
}
public override void Detach()
{
base.Detach();
OnDetaching();
}
protected virtual void OnAttached()
{
}
protected virtual void OnDetaching()
{
}
}
IBehavior 接口来自 Behaviors SDK http://scr.hu/4m4q/pzl07 的 Microsoft.Xaml.Interactivity 命名空间
用法:
<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;
}
}
【讨论】:
content.VerticalAlignment = VerticalAlignment.Top 时,它解决了这个问题。所以它是关于内容的位置网,一旦它被调整大小。也许调整父容器的大小比调整内容更好。