【发布时间】:2018-07-31 21:49:07
【问题描述】:
我正在使用Xamarin.Forms 为android 和iOS 平台创建一个自定义FAB 按钮。
一切正常,但现在我希望能够以某种方式从FAB 按钮的最近父级接收scrolled 事件(如果有),因此我将能够在用户隐藏FAB 按钮时正在滚动,然后在滚动完成 2 秒后再次显示?
父项可以是普通的ScrollView 项目,也可以是ListView。
这可以通过每个平台的相同自定义渲染器来实现吗?或者,这甚至可以实现吗?
这是我到目前为止所做的:
-
FabButton类:
public partial class FabButton : Button
{
public static BindableProperty ButtonColorProperty = BindableProperty.Create(nameof(ButtonColor), typeof(Color), typeof(FabButton), Color.Accent);
public Color ButtonColor
{
get => (Color)GetValue(ButtonColorProperty);
set => SetValue(ButtonColorProperty, value);
}
public FabButton()
{
InitializeComponent();
}
}
-
iOS自定义渲染器:public class FabButtonRenderer:ButtonRenderer { protected override void OnElementChanged(ElementChangedEventArgs<Button> e) { base.OnElementChanged(e); if(e.NewElement == null) return; if (Element.WidthRequest <= 0) Element.WidthRequest = 60; if (Element.HeightRequest <= 0) Element.HeightRequest = 60; if (Element.Margin == new Thickness(0, 0, 0, 0)) Element.Margin = new Thickness(0, 0, 20, 20); Element.CornerRadius = (int) Element.WidthRequest / 2; Element.BorderWidth = 0; Element.Text = null; Control.BackgroundColor = ((FabButton) Element).ButtonColor.ToUIColor(); } public override void Draw(CGRect rect) { base.Draw(rect); Layer.ShadowRadius = 0.2f; Layer.ShadowColor = UIColor.Black.CGColor; Layer.ShadowOffset = new CGSize(1, 1); Layer.ShadowOpacity = 0.80f; Layer.ShadowPath = UIBezierPath.FromOval(Layer.Bounds).CGPath; Layer.MasksToBounds = false; } protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e) { base.OnElementPropertyChanged(sender, e); if (e.PropertyName == nameof(FabButton.ButtonColor)) Control.BackgroundColor = ((FabButton) Element).ButtonColor.ToUIColor(); } } -
android自定义渲染器:public class FabButtonRenderer: Xamarin.Forms.Platform.Android.AppCompat.ViewRenderer<FabButton,FloatingActionButton> { public static void InitRenderer() { } public FabButtonRenderer(Context context):base(context) { } protected override void OnElementChanged(ElementChangedEventArgs<FabButton> e) { base.OnElementChanged(e); if (e.NewElement == null) return; if (e.NewElement.HeightRequest <= 0) e.NewElement.HeightRequest = 85; if (e.NewElement.WidthRequest <= 0) e.NewElement.WidthRequest = 75; if (e.NewElement.Margin.Equals(new Thickness(0, 0, 0, 0))) e.NewElement.Margin = new Thickness(0, 0, 5, 10); var fabButton = new FloatingActionButton(Context); ViewCompat.SetBackgroundTintList(fabButton, ColorStateList.ValueOf(Element.ButtonColor.ToAndroid())); fabButton.UseCompatPadding = true; if (!string.IsNullOrEmpty(Element.Image?.File)) fabButton.SetImageDrawable(Context.GetDrawable(Element.Image.File)); fabButton.Click += FabButton_Clicked; SetNativeControl(fabButton); } protected override void OnLayout(bool changed, int l, int t, int r, int b) { base.OnLayout(changed, l, t, r, b); Control.BringToFront(); } protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e) { if (e.PropertyName == nameof(Element.ButtonColor)) ViewCompat.SetBackgroundTintList(Control, ColorStateList.ValueOf(Element.ButtonColor.ToAndroid())); if (e.PropertyName == nameof(Element.Image)) { if (!string.IsNullOrEmpty(Element.Image?.File)) Control.SetImageDrawable(Context.GetDrawable(Element.Image.File)); } base.OnElementPropertyChanged(sender, e); } public void FabButton_Clicked(object sender, EventArgs e) { Element.SendClicked(); } }
【问题讨论】:
标签: c# mvvm xamarin.forms xamarin.ios xamarin.android