diff --git a/Ooui.Forms/Renderers/ScrollViewRenderer.cs b/Ooui.Forms/Renderers/ScrollViewRenderer.cs index c364899..bbd3522 100644 --- a/Ooui.Forms/Renderers/ScrollViewRenderer.cs +++ b/Ooui.Forms/Renderers/ScrollViewRenderer.cs @@ -5,18 +5,56 @@ using Xamarin.Forms; namespace Ooui.Forms.Renderers { - public class ScrollViewRenderer : ViewRenderer + public class ScrollViewRenderer : VisualElementRenderer { + bool disposed = false; + protected override void OnElementChanged (ElementChangedEventArgs e) { - base.OnElementChanged (e); + if (e.OldElement != null) { + e.OldElement.ScrollToRequested -= Element_ScrollToRequested; + } - this.Style.Overflow = "scroll"; + if (e.NewElement != null) { + Style.Overflow = "scroll"; + + e.NewElement.ScrollToRequested += Element_ScrollToRequested; + } + + base.OnElementChanged (e); } - protected override void OnElementPropertyChanged (object sender, PropertyChangedEventArgs e) + protected override void Dispose (bool disposing) { - base.OnElementPropertyChanged (sender, e); + base.Dispose (disposing); + + if (disposing && !disposed) { + if (Element != null) { + Element.ScrollToRequested -= Element_ScrollToRequested; + } + disposed = true; + } + } + + void Element_ScrollToRequested (object sender, ScrollToRequestedEventArgs e) + { + var oe = (ITemplatedItemsListScrollToRequestedEventArgs)e; + var item = oe.Item; + var group = oe.Group; + if (e.Mode == ScrollToMode.Position) { + Send (Ooui.Message.Set (Id, "scrollTop", e.ScrollY)); + Send (Ooui.Message.Set (Id, "scrollLeft", e.ScrollX)); + } + else { + switch (e.Position) { + case ScrollToPosition.Start: + Send (Ooui.Message.Set (Id, "scrollTop", 0)); + break; + case ScrollToPosition.End: + Send (Ooui.Message.Set (Id, "scrollTop", new Ooui.Message.PropertyReference { TargetId = Id, Key = "scrollHeight" })); + break; + } + } } } }