Add ScrollTo support to ScrollView

This commit is contained in:
Frank A. Krueger 2018-04-16 12:38:10 -07:00
parent 27745d9d35
commit 7ee02c4b38
No known key found for this signature in database
GPG Key ID: 0471C67474FFE664
1 changed files with 43 additions and 5 deletions

View File

@ -5,18 +5,56 @@ using Xamarin.Forms;
namespace Ooui.Forms.Renderers namespace Ooui.Forms.Renderers
{ {
public class ScrollViewRenderer : ViewRenderer<ScrollView, Div> public class ScrollViewRenderer : VisualElementRenderer<ScrollView>
{ {
bool disposed = false;
protected override void OnElementChanged (ElementChangedEventArgs<ScrollView> e) protected override void OnElementChanged (ElementChangedEventArgs<ScrollView> e)
{ {
if (e.OldElement != null) {
e.OldElement.ScrollToRequested -= Element_ScrollToRequested;
}
if (e.NewElement != null) {
Style.Overflow = "scroll";
e.NewElement.ScrollToRequested += Element_ScrollToRequested;
}
base.OnElementChanged (e); base.OnElementChanged (e);
this.Style.Overflow = "scroll";
} }
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;
}
}
} }
} }
} }