【问题标题】:xamarin.forms scrollview keyboard appears and button also scrollxamarin.forms scrollview 键盘出现并且按钮也滚动
【发布时间】:2015-05-22 18:33:43
【问题描述】:

您好,我有一个奇怪的问题。

我使用 xamarin.forms 为数据输入定义了几个标签和文本框。 我将它们包装到滚动视图中,以便当键盘出现时,它们应该滚动。

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

以下是我的代码:

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;

任何想法有什么问题?

【问题讨论】:

    标签: c# xamarin.forms


    【解决方案1】:

    您必须更改布局才能达到目标:
    - 定义一个“Main-StackLayout”
    - 为您的按钮创建一个“Button-StackLayout”(应该保持在顶部)
    - 将您的按钮添加到“Button-StackLayout”
    - 将“button-StackLayout”添加到“Main-StackLayout”
    - 将 ScrollView 添加到“Main-StackLayout”
    - 将页面内容设置为“Main-StackLayout”
    这应该可行:按钮保持在顶部,其中(仅)可以滚动 ScrollView。

    【讨论】:

      猜你喜欢
      • 2015-02-02
      • 1970-01-01
      • 2018-12-08
      • 1970-01-01
      • 2020-03-02
      • 1970-01-01
      • 2019-04-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多