xamarin.forms滚动视图键盘出现,并且按钮也滚动 [英] xamarin.forms scrollview keyboard appears and button also scroll

查看:170
本文介绍了xamarin.forms滚动视图键盘出现,并且按钮也滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个奇怪的问题.

我使用xamarin.forms定义了一些标签和文本框用于数据输入. 我将它们包裹在滚动视图中,以便在出现键盘时它们也可以滚动.

I defined few labels and textbox for data entry purpose using xamarin.forms. I wrapped them into scroll view so that when keyboard appears, they should scroll.

工作正常.具有焦点的控件滚动到顶部,并在获得焦点时出现键盘.但我在表单底部也有几个按钮.现在的问题是,每当出现键盘时,我的底部按钮也会滚动.看起来很奇怪由于按钮用于提交或取消,因此应留在底部.

It is working fine. The control which has focus scroll to top and the keyboard appears when it get focus. but i also have couple of buttons at the bottom of my form. Now, the problem is, whenever keyboard appears, my bottom buttons are also scrolled. which looks weird. As button are for submit or cancel, it should be stay there in bottom.

以下是我的代码:

var firstNameLabel = new Label { HorizontalOptions = LayoutOptions.Fill };              
firstNameLabel.Text = "First Name";

            var firstName = new Entry() { HorizontalOptions = LayoutOptions.FillAndExpand };
            firstName.SetBinding (Entry.TextProperty,MyViewModel.FirstNamePropertyName);

            var lastNameLabel = new Label { HorizontalOptions = LayoutOptions.Fill};
            lastNameLabel.Text = "Last Name";
            var lastName = new Entry() { HorizontalOptions = LayoutOptions.FillAndExpand };
            lastName.SetBinding (Entry.TextProperty, MyViewModel.LastNamePropertyName);
---- other fields

Button btnSubmit  = new Button
            {
                HorizontalOptions = LayoutOptions.Fill,
                BackgroundColor = Color.FromHex("#22498a"),
                TextColor = Color.White,
                Text = "Submit"
            };

var cancelButton = new Button { Text = Cancel", BackgroundColor = Color.FromHex("0d9c00"), TextColor = Color.White };
            contactUsButton.Clicked +=  (object sender, EventArgs e) =>
            {
                // cancel operation
            };

var cotrolStakeLayout = new StackLayout () {
                Padding = new Thickness(Device.OnPlatform(5, 5, 5),0 , Device.OnPlatform(5, 5, 5), 0), //new Thickness(5,0,5,0),
                VerticalOptions = LayoutOptions.FillAndExpand, 
                HorizontalOptions = LayoutOptions.Fill,
                Orientation = StackOrientation.Vertical,
                Children = { firstNameLabel, firstName, lastNameLabel, lastName, -- and other fields}
            };

var scrollableContentLayout = new ScrollView (){ 
                Content = cotrolStakeLayout,
                 Orientation = ScrollOrientation.Vertical,
                 HorizontalOptions = LayoutOptions.Fill,
                 VerticalOptions = LayoutOptions.Fill
            };


var buttonLayout = new StackLayout (){ 
                Padding = new Thickness(Device.OnPlatform(5, 5, 5),0 , Device.OnPlatform(5, 5, 5), 0), //new Thickness(5,0,5,0),
                HorizontalOptions = LayoutOptions.Fill,
                VerticalOptions = LayoutOptions.Fill, 
                Orientation = StackOrientation.Vertical,
                Children= { btnSubmit , cancelButton }
            };


var nameLayout = new StackLayout()
            {
                HorizontalOptions = LayoutOptions.Fill,
                VerticalOptions = LayoutOptions.Fill, 
                Orientation = StackOrientation.Vertical,
                Children = {scrollableContentLayout,buttonLayout}
            };

return nameLayout;

任何想法有什么问题吗?

Any ideas what is wrong with it?

推荐答案

您必须更改布局才能达到目标:
-定义"Main-StackLayout"
-为您的Button创建一个"Button-StackLayout"(应该位于顶部)
-将您的按钮添加到"Button-StackLayout"中
-将"button-StackLayout添加到" Main-StackLayout"中
-将ScrollView添加到"Main-StackLayout"
-将页面内容设置为"Main-StackLayout"
这应该可以工作:按钮停留在顶部,滚动视图(仅)可以在此处滚动.

You have to change your layout to reach your target:
- Define a "Main-StackLayout"
- Create a "Button-StackLayout" for your Buttons (that should stay on top)
- Add your Buttons to the "Button-StackLayout"
- Add the "button-StackLayout to the "Main-StackLayout"
- Add the ScrollView to to "Main-StackLayout"
- Set content of the page to the "Main-StackLayout"
This should work: buttons stays on top, where (only) the ScrollView can be scrolled.

这篇关于xamarin.forms滚动视图键盘出现,并且按钮也滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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