Xamarin.Forms 中的浮动操作按钮

Floating Action Button in Xamarin.Forms

我已经在 Xamarin.Forms Portable 中完成了我的应用程序主页。

现在我想在我的 Android 项目中添加一个浮动操作按钮!

有什么方法可以在我现有的主页中添加 Android 的 FAB,它是在 Xamarin.Forms Portable.

中编码的

我想为 Android 创建一个单独的主页并将其添加为 android 的主页面?

感谢和问候。

官方支持库出来之前我把FAB移植过来了

我的 GitHub 存储库中现在有一个 Xamarin.Forms 示例可供您使用:https://github.com/jamesmontemagno/FloatingActionButton-for-Xamarin.Android

构建自定义控件 为了使 FAB 的属性在 Xamarin.Forms 中可绑定,我们需要一个具有可绑定属性的自定义控件。

public class FloatingActionButtonView : View
{
    public static readonly BindableProperty ImageNameProperty = BindableProperty.Create<FloatingActionButtonView,string>( p => p.ImageName, string.Empty);
    public string ImageName 
    {
      get { return (string)GetValue (ImageNameProperty); } 
      set { SetValue (ImageNameProperty, value); } 
    }

    public static readonly BindableProperty ColorNormalProperty = BindableProperty.Create<FloatingActionButtonView,Color>( p => p.ColorNormal, Color.White);
    public Color ColorNormal 
    {
      get { return (Color)GetValue (ColorNormalProperty); } 
      set { SetValue (ColorNormalProperty, value); } 
    }

    public static readonly BindableProperty ColorPressedProperty = BindableProperty.Create<FloatingActionButtonView,Color>( p => p.ColorPressed, Color.White);
    public Color ColorPressed 
    {
      get { return (Color)GetValue (ColorPressedProperty); } 
      set { SetValue (ColorPressedProperty, value); } 
    }

    public static readonly BindableProperty ColorRippleProperty = BindableProperty.Create<FloatingActionButtonView,Color>( p => p.ColorRipple, Color.White);
    public Color ColorRipple 
    {
      get { return (Color)GetValue (ColorRippleProperty); } 
      set { SetValue (ColorRippleProperty, value); } 
    }
    ...
}

然后我们将每个 属性 映射到本机 FAB 控件上相应的 属性。

附加渲染器

如果我们想在Xamarin.Forms中使用原生控件,我们需要一个渲染器。为简单起见,让我们使用 ViewRenderer。此渲染器会将我们的自定义 FloatingActionButtonView 映射到 Android.Widget.FrameLayout.

public class FloatingActionButtonViewRenderer : ViewRenderer<FloatingActionButtonView, FrameLayout>
{
    ...
    private readonly Android.Content.Context context;
    private readonly FloatingActionButton fab;

    public FloatingActionButtonViewRenderer()
    {
        context = Xamarin.Forms.Forms.Context;
        fab = new FloatingActionButton(context);
        ...
    }

    protected override void OnElementChanged(ElementChangedEventArgs<FloatingActionButtonView> e)
    {
        base.OnElementChanged(e);

        if (e.OldElement != null || this.Element == null)
            return;

        if (e.OldElement != null)
            e.OldElement.PropertyChanged -= HandlePropertyChanged;

        if (this.Element != null) {
            //UpdateContent ();
            this.Element.PropertyChanged += HandlePropertyChanged;
        }

        Element.Show = Show;
        Element.Hide = Hide;

        SetFabImage(Element.ImageName);

        fab.ColorNormal = Element.ColorNormal.ToAndroid();
        fab.ColorPressed = Element.ColorPressed.ToAndroid();
        fab.ColorRipple = Element.ColorRipple.ToAndroid();

        var frame = new FrameLayout(Forms.Context);
        frame.RemoveAllViews();
        frame.AddView(fab);

        SetNativeControl (frame);
    }

    public void Show(bool animate = true)
    {
        fab.Show(animate);
    }

    public void Hide(bool animate = true)
    {
        fab.Hide(animate);
    }

    void HandlePropertyChanged (object sender, System.ComponentModel.PropertyChangedEventArgs e)
    {
        if (e.PropertyName == "Content") {
            Tracker.UpdateLayout ();
        } 
        else if (e.PropertyName == FloatingActionButtonView.ColorNormalProperty.PropertyName) 
        {
            fab.ColorNormal = Element.ColorNormal.ToAndroid();
        } 
        else if (e.PropertyName == FloatingActionButtonView.ColorPressedProperty.PropertyName) 
        {
            fab.ColorPressed = Element.ColorPressed.ToAndroid();
        } 
        else if (e.PropertyName == FloatingActionButtonView.ColorRippleProperty.PropertyName) 
        {
            fab.ColorRipple = Element.ColorRipple.ToAndroid();
        }
        ...
    }

    void SetFabImage(string imageName)
    {
        if(!string.IsNullOrWhiteSpace(imageName))
        {
            try
            {
                var drawableNameWithoutExtension = Path.GetFileNameWithoutExtension(imageName);
                var resources = context.Resources;
                var imageResourceName = resources.GetIdentifier(drawableNameWithoutExtension, "drawable", context.PackageName);
                fab.SetImageBitmap(Android.Graphics.BitmapFactory.DecodeResource(context.Resources, imageResourceName));
            }
            catch(Exception ex)
            {
                throw new FileNotFoundException("There was no Android Drawable by that name.", ex);
            }
        }
    }
}

齐心协力

好的!我们已经构建了自定义控件,并将其映射到渲染器。最后一步是在我们的视图中布置控件。

public class MainPage : ContentPage
{
    public MainPage()
    {
        var fab = new FloatingActionButtonView() {
            ImageName = "ic_add.png",
            ColorNormal = Color.FromHex("ff3498db"),
            ColorPressed = Color.Black,
            ColorRipple = Color.FromHex("ff3498db")
        };

        // Main page layout
        var pageLayout = new StackLayout {
            Children = 
            {
                new Label {
                    VerticalOptions = LayoutOptions.CenterAndExpand,
                    XAlign = TextAlignment.Center,
                    Text = "Welcome to Xamarin Forms!" 
                }
            }};

        var absolute = new AbsoluteLayout() { 
            VerticalOptions = LayoutOptions.FillAndExpand, 
            HorizontalOptions = LayoutOptions.FillAndExpand };

        // Position the pageLayout to fill the entire screen.
        // Manage positioning of child elements on the page by editing the pageLayout.
        AbsoluteLayout.SetLayoutFlags(pageLayout, AbsoluteLayoutFlags.All);
        AbsoluteLayout.SetLayoutBounds(pageLayout, new Rectangle(0f, 0f, 1f, 1f));
        absolute.Children.Add(pageLayout);

        // Overlay the FAB in the bottom-right corner
        AbsoluteLayout.SetLayoutFlags(fab, AbsoluteLayoutFlags.PositionProportional);
        AbsoluteLayout.SetLayoutBounds(fab, new Rectangle(1f, 1f, AbsoluteLayout.AutoSize, AbsoluteLayout.AutoSize));
        absolute.Children.Add(fab);

        Content = absolute;
    }
}

Github 上的完整代码:Floating Action Button Xamarin.Forms