【问题标题】:Can't scroll till the end of the results when the keyborad is opened (Windows Phone)打开键盘时无法滚动到结果末尾(Windows Phone)
【发布时间】:2016-03-03 06:30:12
【问题描述】:

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

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

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

【问题讨论】:

    标签: xaml layout windows-phone-8.1


    【解决方案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 时,它解决了这个问题。所以它是关于内容的位置网,一旦它被调整大小。也许调整父容器的大小比调整内容更好。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-10-16
    • 1970-01-01
    • 2020-12-06
    • 2020-05-21
    • 2022-07-08
    • 1970-01-01
    • 2020-08-21
    相关资源
    最近更新 更多