在Xamarin.Forms中水平滚动ScrollView
问题描述:
我使用Xamarin.Forms并创建了一个ScrollView,它包含一个水平的StackLayout。我想能够水平滚动,所以我设置:在Xamarin.Forms中水平滚动ScrollView
Orientation = ScrollOrientation.Horizontal;
但我没有得到水平滚动。 StackLayout的内容比屏幕更宽,并且我看到内容被剪切在边缘。
如何用Xamarin.Forms实现水平滚动?
答
这是我得到了它,如果你正在使用的模板,在Visual Studio 2013年Xamarin应用工作
var scrollView = ScrollView
{
HorizontalOptions = LayoutOptions.Fill,
Orientation = ScrollOrientation.Horizontal,
Content = new StackLayout{
Orientation = StackOrientation.Horizontal,
Children = {}
}
};
答
,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,
};
}
}
代码将在Android上正常工作。
对于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;
}
}
}
}
你可以发布你的代码来创建ScrollView并使用StackLayout设置其内容吗? – Pedro