在Xamarin.Forms ScrollView中水平滚动

19

我正在使用Xamarin.Forms并创建了一个ScrollView,其中包含一个水平的StackLayout。我希望能够水平滚动,因此我设置了:

Orientation  = ScrollOrientation.Horizontal;

但是我无法横向滚动。 StackLayout 的内容比屏幕宽,我看到内容在边缘被裁剪。

如何在 Xamarin.Forms 中实现横向滚动?


你能否发布一下创建ScrollView并使用StackLayout设置其内容的代码? - Pedro
3个回答

31

这是我让它正常工作的方法

      var scrollView = ScrollView
        {
            HorizontalOptions = LayoutOptions.Fill,
            Orientation = ScrollOrientation.Horizontal,

            Content = new StackLayout{
               Orientation = StackOrientation.Horizontal,
               Children = {}
            }
        };

3
确实,我们必须在StackLayout上设置Orientation = ScrollOrientation.Horizontal。谢谢。 - Barton
我有同样的问题,但我是通过编程方式添加stacklayout子项的。下面的答案对我不起作用! - Alireza Akbari
我认为关键是:Orientation = ScrollOrientation.Horizontal。当然,您应该将HorizontalOptions设置为Fill或FillAndExpand。但是,如果您没有设置Orientation = ScrollOrientation.Horizontal,则不会显示水平滚动条。 - peter70

2
这个Nuget包可以使用: https://github.com/SuavePirate/DynamicStackLayout 其中,属性Words是一个字符串列表。
    <ScrollView Orientation="Horizontal" HorizontalOptions="FillAndExpand">
        <dynamicStackLayout:DynamicStackLayout ItemsSource="{Binding Words}" HorizontalOptions="Fill" Orientation="Horizontal" Padding="10, -0, 50, 10">
            <dynamicStackLayout:DynamicStackLayout.ItemTemplate>
                <DataTemplate>
                    <StackLayout BackgroundColor="Gray" WidthRequest="80" HeightRequest="80">
                        <Label Text="{Binding .}" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand" VerticalTextAlignment="Center" HorizontalTextAlignment="Center" />
                    </StackLayout>
                </DataTemplate>
            </dynamicStackLayout:DynamicStackLayout.ItemTemplate>
        </dynamicStackLayout:DynamicStackLayout>
    </ScrollView>

我希望这可以帮助您 :)

0
如果您正在使用Visual Studio 2013中的Xamarin应用程序模板,则Xamarin.Forms的版本有点过时,不支持滚动。要解决此问题,只需运行nuget 'update-package'并添加此代码即可。
public class MainPage : ContentPage
{
    public MainPage()
    {
        Label label = new Label {
            Text = "This is a very long label which I expect to scroll horizontally because it's in a ScrollView.",
            Font = Font.SystemFontOfSize(24),
        };

        this.Content = new ScrollView {
            Content = label,
            Orientation = ScrollOrientation.Horizontal, 
        };
    }
}

这段代码在安卓上可以正常运行。

对于iOS,代码也能按预期工作。

不幸的是,目前在WP8上存在一个错误,解决方法是添加自定义渲染器。

using System.Windows.Controls;
using App2.WinPhone;
using Xamarin.Forms;
using Xamarin.Forms.Platform.WinPhone;

[assembly: ExportRenderer(typeof(ScrollView), typeof(FixedSVRenderer))]

namespace App2.WinPhone
{
    public sealed class FixedSVRenderer : ScrollViewRenderer
    {
        protected override void OnModelSet()
        {
            base.OnModelSet();

            if (Model.Orientation == ScrollOrientation.Horizontal)
            {
                // Enable horiz-scrolling
                Control.HorizontalScrollBarVisibility = ScrollBarVisibility.Auto;
            }
        }
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接