gpt4 book ai didi

c# - 父级滚动时隐藏 float 操作按钮

转载 作者:行者123 更新时间:2023-11-30 23:00:25 24 4
gpt4 key购买 nike

我正在使用 Xamarin.FormsandroidiOS 平台创建自定义 FAB 按钮。

一切正常,但现在我希望能够以某种方式从 FAB 按钮的最近父级接收 scrolled 事件(如果有的话),这样我就可以在用户滚动时隐藏 FAB 按钮,然后在滚动完成 2 秒后再次显示它?

父项可以是普通的 ScrollView 项,也可以是 ListView
这可以通过为每个平台使用相同的自定义渲染器来实现吗?或者,这甚至可以实现吗?

这是我目前所做的:

  1. 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();
}
}
  1. 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();
    }
    }
  2. 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();
    }
    }

最佳答案

我认为 Scrollview 的“滚动”事件可以帮助您,您将获得 X 和 Y 轴值,基于它您必须在滚动时隐藏您的 fab 按钮。2 秒后,您可以使用“Device.StartTimer”。

关于c# - 父级滚动时隐藏 float 操作按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51579967/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com