using System; using System.ComponentModel; using ElmSharp; namespace Xamarin.Forms.Platform.Tizen { /// /// This class provides a Renderer for a ScrollView widget. /// public class ScrollViewRenderer : ViewRenderer { EvasObject _content; /// /// Initializes a new instance of the class. /// public ScrollViewRenderer() { RegisterPropertyHandler("Content", FillContent); } /// /// Handles the element change event. /// /// Event arguments. protected override void OnElementChanged(ElementChangedEventArgs e) { if (Control == null) { var scrollView = new Scroller(Forms.Context.MainWindow); SetNativeControl(scrollView); } if (e.OldElement != null) { Control.Scrolled -= ScrollViewScrolledHandler; (e.OldElement as IScrollViewController).ScrollToRequested -= ScrollRequestHandler; } if (e.NewElement != null) { Control.Scrolled += ScrollViewScrolledHandler; (e.NewElement as IScrollViewController).ScrollToRequested += ScrollRequestHandler; } UpdateAll(); base.OnElementChanged(e); } protected override void Dispose(bool disposing) { if (null != Element) { (Element as IScrollViewController).ScrollToRequested -= ScrollRequestHandler; } base.Dispose(disposing); } void FillContent() { if (_content != null) { Control.SetContent(null, true); } _content = Platform.GetOrCreateRenderer(Element.Content).NativeView; if (_content != null) { Control.SetContent(_content, true); } } void UpdateAll() { UpdateOrientation(); } void UpdateOrientation() { switch (Element.Orientation) { case ScrollOrientation.Horizontal: Control.ScrollBlock = ScrollBlock.Vertical; Control.HorizontalScrollBarVisiblePolicy = ScrollBarVisiblePolicy.Auto; Control.VerticalScrollBarVisiblePolicy = ScrollBarVisiblePolicy.Invisible; break; case ScrollOrientation.Vertical: Control.ScrollBlock = ScrollBlock.Horizontal; Control.HorizontalScrollBarVisiblePolicy = ScrollBarVisiblePolicy.Invisible; Control.VerticalScrollBarVisiblePolicy = ScrollBarVisiblePolicy.Auto; break; default: Control.ScrollBlock = ScrollBlock.None; Control.HorizontalScrollBarVisiblePolicy = ScrollBarVisiblePolicy.Auto; Control.VerticalScrollBarVisiblePolicy = ScrollBarVisiblePolicy.Auto; break; } } /// /// An event raised on element's property change. /// /// Sender. /// Event arguments protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e) { if (ScrollView.OrientationProperty.PropertyName == e.PropertyName) { UpdateOrientation(); } base.OnElementPropertyChanged(sender, e); } void ScrollViewScrolledHandler(object sender, EventArgs e) { var region = Control.CurrentRegion; ((IScrollViewController)Element).SetScrolledPosition(region.X, region.Y); } void ScrollRequestHandler(object sender, ScrollToRequestedEventArgs e) { var x = e.ScrollX; var y = e.ScrollY; if (e.Mode == ScrollToMode.Element) { Point itemPosition = (Element as IScrollViewController).GetScrollPositionForElement(e.Element as VisualElement, e.Position); x = itemPosition.X; y = itemPosition.Y; } Rect region = new Rectangle(x, y, Element.Width, Element.Height).ToPixel(); Control.ScrollTo(region, e.ShouldAnimate); } } }