【问题标题】:Two Dimensional Scroll in Xamarin.FormsXamarin.Forms 中的二维滚动
【发布时间】:2014-12-15 05:27:43
【问题描述】:

我是 xamarin.forms 的新手,我正在尝试在 Windows 中创建一个带有二维滚动的自己的滚动查看器。我与 Android 合作过,我通过从 FrameLayout 派生并使用所有 action_down 将滚动条初始化到其中实现了相同的行为,它很好地出现了。

但现在真正的问题是我找不到任何类似于 Android 的东西(在 xamarin.forms 默认布局和控件中),而且我真的很难想出一个粗略的想法。

场景是在视图中加载几十个文本块,就像网格布局一样,它应该垂直、水平和对角滚动。

我是否应该通过为每个平台创建单独的滚动来使用渲染器来处理它,或者如果可能的话可以在 xamarin.forms 中使用它,你能给它一个提示吗?

提前致谢。

迪内什·库马尔

【问题讨论】:

    标签: android xamarin.ios xamarin.android windows-phone-8.1 xamarin.forms


    【解决方案1】:

    没有自定义渲染器是不可能的。除了在 iOS 中,该行为在那里按预期工作。但是,这目前不适用于 Windows Phone。

    这是我编写的示例课程。只需将一些内容传递给包含换行符的文本属性。我会推荐来自http://i-tools.org/lorem 的东西,只输出纯文本,每行 1 个句子,每段 1 行,如果你不知道怎么想,每行都以 '\n' 结尾。

    public class BothDirectionsScrollPage : ContentPage
    {
        public BothDirectionsScrollPage(string title, string text)
        {
            this.Title = title;
            var count = 0;
            var splittext = text.Split('\n');
            var stack = new StackLayout();
            foreach (var line in splittext)
            {
                var label = new Label
                {
                    Font = Font.SystemFontOfSize(NamedSize.Medium),
                    LineBreakMode = LineBreakMode.NoWrap,
                    FormattedText = new FormattedString()
                    {
                        Spans = {new Span {Text = line}}
                    }
                };
                stack.Children.Add(label);
                count++;
            }
            var scroll = new ScrollView
            {
                Padding = 0,
                WidthRequest = 500,
                Content = stack,
                VerticalOptions = LayoutOptions.FillAndExpand,
                HorizontalOptions = LayoutOptions.FillAndExpand,
                Orientation = ScrollOrientation.Horizontal
            };
            Content = new ScrollView()
            {
                Content = scroll,
                VerticalOptions = LayoutOptions.FillAndExpand,
                HorizontalOptions = LayoutOptions.FillAndExpand,
                Orientation = ScrollOrientation.Vertical,
    
            };
        }
    }
    

    这是您需要包含在您的 Android 项目中的自定义渲染器。

    using System;
    using Android.Views;
    using Your.Namespace.Droid.Renderers
    using Xamarin.Forms;
    using Xamarin.Forms.Platform.Android;
    
    [assembly: ExportRenderer(typeof(ScrollView), typeof(CustomScrollViewRenderer))]
    
    namespace Your.Namespace.Droid.Renderers
    {
            /// <summary>
        /// Used to allow horizontal and vertical scroll views. Xamarin.Android does not bubble the     events down correctly.
        /// </summary>
        public class CustomScrollViewRenderer : ScrollViewRenderer
        {
    
            private float StartX, StartY;
            private int IsHorizontal = -1;
    
            protected override void OnElementChanged(VisualElementChangedEventArgs e)
            {
                base.OnElementChanged(e);
                if (((ScrollView) e.NewElement).Orientation == ScrollOrientation.Horizontal) IsHorizontal = 1;
    
            }
    
            public override bool DispatchTouchEvent(MotionEvent e)
            {
                switch (e.Action)
                {
                    case MotionEventActions.Down:
                        StartX = e.RawX;
                        StartY = e.RawY;
                        this.Parent.RequestDisallowInterceptTouchEvent(true);
                        break;
                    case MotionEventActions.Move:
                        if (IsHorizontal*Math.Abs(StartX - e.RawX) < IsHorizontal*Math.Abs(StartY - e.RawY))
                            this.Parent.RequestDisallowInterceptTouchEvent(false);
                        break;
                    case MotionEventActions.Up:
                        this.Parent.RequestDisallowInterceptTouchEvent(false);
                        break;
                }
    
                return base.DispatchTouchEvent(e);
            }
    
        }
    }
    

    感谢 Xavyer,http://forums.xamarin.com/discussion/20834/horizontal-scrollview-within-vertical-scrollview

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-07-14
      • 1970-01-01
      • 2017-08-26
      • 1970-01-01
      • 1970-01-01
      • 2014-08-14
      • 2020-09-25
      相关资源
      最近更新 更多