Scroll editor in Xamarin Forms into view

前端 未结 6 1120
天涯浪人
天涯浪人 2021-02-02 01:49

Using Xamarin Forms, consider the Xaml below.


   

        
6条回答
  •  不知归路
    2021-02-02 02:12

    I just stumbled upon this question when writing a tiny chat application, which basically contains a scrollable message list, a text entry and a send button:

    The problem with the previously posted solution is that you'd need to nest two scroll views, which is not recommended by the Xamarin.Forms documentation. To prevent the keyboard from hiding the entry, I found the following hack:

    I'm adding a placeholder at the end of the main stack layout. Depending on whether the entry is focussed (i.e. the keyboard is visible or not) the height of the placeholder is set to 0 or the keyboard height.

            // HACK: make entry visible when keyboard open
            var placeholder = new BoxView {
                HeightRequest = 0,
            };
            entry.Focused += (sender, e) => placeholder.HeightRequest = 210;
            entry.Unfocused += (sender, e) => placeholder.HeightRequest = 0;
    
            Content = new StackLayout {
                VerticalOptions = LayoutOptions.Fill,
                Padding = 5,
                Children = {
                    whoTable,
                    messageScrollView,
                    new StackLayout {
                        Orientation = StackOrientation.Horizontal,
                        VerticalOptions = LayoutOptions.End,
                        HeightRequest = 70,
                        Children = {
                            entry,
                            sendButton,
                        },
                    },
                    placeholder,
                },
            };
    

    Of course, this is not perfect. Especially the hard-coded keyboard height should be implemented more elegantly. And probably you should apply it on iOS only, not on Android.

提交回复
热议问题