2014-06-24 49 views
8

我使用Xamarin.Forms并创建了一个ScrollView,它包含一个水平的StackLayout。我想能够水平滚动,所以我设置:在Xamarin.Forms中水平滚动ScrollView

Orientation = ScrollOrientation.Horizontal; 

但我没有得到水平滚动。 StackLayout的内容比屏幕更宽,并且我看到内容被剪切在边缘。

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

+0

你可以发布你的代码来创建ScrollView并使用StackLayout设置其内容吗? – Pedro

回答

12

这是我得到了它,如果你正在使用的模板,在Visual Studio 2013年Xamarin应用工作

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

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

确实,我们必须在StackLayout上有Orientation = ScrollOrientation.Horizo​​ntal。谢谢。 – Barton

+0

我有同样的问题,但我以编程方式添加stacklayout儿童。和下面的答案不适合我! – AlirezaXX

0

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