打开键盘后无法滚动到结果的结尾(Windows Phone) [英] Can't scroll till the end of the results when the keyborad is opened (Windows Phone)

查看:62
本文介绍了打开键盘后无法滚动到结果的结尾(Windows Phone)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am developing Windows Phone app and I've got this issue: I have a list control which displays my search results, but when the keyboard is opened some of my results aren't visible because of my keyboard...

有没有一种方法可以将控件缩小到键盘边框附近?为了查看所有结果.

Is there a way to shrink the control till keyboard border? In order to see all the results.

即使打开键盘,我也要滚动到结果的结尾.

I want to scroll till the end of the results even when the keyboard is opened.

推荐答案

有我的解决方案

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

IBehavior interface is from Microsoft.Xaml.Interactivity namespace from Behaviors SDK 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>

或相同的功能,但没有任何行为.刚刚添加到页面代码后面.

Or the same functionality but without behavior. Just added to page code behind.

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

这篇关于打开键盘后无法滚动到结果的结尾(Windows Phone)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆