1. 程式人生 > >xamarin.form scrollView

xamarin.form scrollView

<ContentPage.Content>
        <ScrollView Orientation="Horizontal">
            <StackLayout Orientation="Horizontal">
                <Label Text="123123123"></Label>
            </StackLayout>
        </ScrollView>
    </ContentPage.Content>

  其中有1個比較重要的屬性Orientation可以改變滾動方向,如果你想通過程式碼修改當前滾動條的位置,可以使用scroll.ScrollToAsync方法來實現

如果你不想顯示滾動條,那麼只能在各自的Android和IOS平臺實現

Android平臺

[assembly:ExportRenderer(typeof(Xamarin.Forms.ScrollView),typeof(ScrollViewNoRender))]
namespace App1.Droid.Render
{
    public class ScrollViewNoRender : ScrollViewRenderer
    {
        protected override void OnElementChanged(VisualElementChangedEventArgs e)
        {
            base.OnElementChanged(e);
            if (e.OldElement != null)
            {
                e.NewElement.PropertyChanged -= NewElement_PropertyChanged;
            }
            if (e.NewElement != null)
            {
                e.NewElement.PropertyChanged += NewElement_PropertyChanged;
            }
        }

        private void NewElement_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (ChildCount > 0)
            {
                GetChildAt(0).HorizontalScrollBarEnabled = false;
                GetChildAt(0).VerticalScrollBarEnabled = false;
            }
        }
    }
}

  IOS平臺

[assembly:ExportRenderer(typeof(ScrollView),typeof(ScrollViewNoRender))]
namespace App1.iOS.Render
{
    public class ScrollViewNoRender : ScrollViewRenderer
    {
        protected override void OnElementChanged(VisualElementChangedEventArgs e)
        {
            base.OnElementChanged(e);

            if (e.OldElement != null)
            {
                e.NewElement.PropertyChanged -= NewElement_PropertyChanged;
            }
            if (e.NewElement != null)
            {
                e.NewElement.PropertyChanged += NewElement_PropertyChanged;
            }
        }

        private void NewElement_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            this.ShowsHorizontalScrollIndicator = false;
            this.ShowsVerticalScrollIndicator = false;
        }
    }
}