在Xamarin.Forms ScrollView中水平滚动

在Xamarin.Forms ScrollView中水平滚动,xamarin.forms,Xamarin.forms,我正在使用Xamarin.Forms并创建了一个ScrollView,其中包含一个水平堆栈布局。我希望能够水平滚动,因此我设置: Orientation = ScrollOrientation.Horizontal; 但我没有水平卷轴。StackLayout的内容比屏幕宽,我看到内容被剪裁在边缘 如何使用Xamarin.Forms实现水平滚动?这就是我如何让它工作的 var scrollView = ScrollView { Horizon

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

Orientation  = ScrollOrientation.Horizontal;
但我没有水平卷轴。StackLayout的内容比屏幕宽,我看到内容被剪裁在边缘


如何使用Xamarin.Forms实现水平滚动?

这就是我如何让它工作的

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

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

如果您在Visual Studio 2013中为Xamarin应用程序使用模板,则Xamarin.Forms的版本有点过时,不支持滚动。要修复此问题,只需获取“更新包”和以下代码

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, 
        };
    }
}
代码在android上运行良好

对于iOS,代码将按预期工作

不幸的是,到目前为止,WP8有一个bug,而黑客就是添加一个自定义的渲染器

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;
            }
        }
    }
}

此nuget包将在以下情况下工作:

属性词是字符串列表:

    <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>


我希望它有帮助:)

您能发布创建ScrollView并使用StackLayout设置其内容的代码吗?事实上,我们必须在StackLayout上设置Orientation=ScrollOrientation.Horizontal。谢谢。我有同样的问题,但我正在以编程方式添加stacklayout子项。下面的答案不适合我!我认为关键是:方向=滚动方向。水平。当然,您应该将水平选项设置为Fill或FillAndExpand。但如果未设置方向=ScrollOrientation.Horizone,则不会显示水平滚动条