打开键盘时无法滚动到结果末尾 (Windows Phone)

Can't scroll till the end of the results when the keyborad is opened (Windows Phone)

我正在开发 Windows Phone 应用程序,我遇到了这个问题: 我有一个显示我的搜索结果的列表控件,但是当键盘打开时,由于我的键盘,我的一些结果不可见...

有没有办法将控件缩小到键盘边框?为了看到所有的结果。

即使键盘打开,我也想滚动到结果的末尾。

有我的解决方案

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 的 Microsoft.Xaml.Interactivity 命名空间 http://scr.hu/4m4q/pzl07

用法:

<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;
    }
}