Xamarin.Forms中的浮动操作按钮

时间:2015-06-22 06:24:44

标签: android xamarin xamarin.android xamarin.forms floating-action-button

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

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

有没有办法在我现有的主页中添加FAB for Android,该主页以Xamarin.Forms Portable编码。

OR

我想为Android创建一个单独的主页,并将其称为Android的主页?

谢谢和问候。

2 个答案:

答案 0 :(得分:5)

在官方支持库出来之前,我将FAB移植到了。

现在我可以使用GitHub仓库中的Xamarin.Forms示例:https://github.com/jamesmontemagno/FloatingActionButton-for-Xamarin.Android

答案 1 :(得分:4)

构建自定义控件 为了使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);
            }
        }
    }
}

全部拉开

OK!我们构建了自定义控件,并将其映射到渲染器。最后一步是在我们看来设置控件。

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