我不会在这里推荐任何第三方库,但通常我们可以使用Custom Renderers来使用PCL的目标平台的本机控件。
对于Android平台的FloatingActionButton,您可以尝试实现一个视图渲染器。
例如在PCL创建一个类名为MyFloatButton
:
public class MyFloatButton : View
{
}
然后在Android项目创建一个名为MyFloatButtonRenderer
例如这样的另一个类:
[assembly: ExportRenderer(typeof(MyFloatButton), typeof(MyFloatButtonRenderer))]
namespace NameSpace.Droid
{
public class MyFloatButtonRenderer : Xamarin.Forms.Platform.Android.ViewRenderer<MyFloatButton, FloatingActionButton>
{
private FloatingActionButton fab;
protected override void OnElementChanged(ElementChangedEventArgs<MyFloatButton> e)
{
base.OnElementChanged(e);
if (Control == null)
{
fab = new FloatingActionButton(Xamarin.Forms.Forms.Context);
fab.LayoutParameters = new LayoutParams(ViewGroup.LayoutParams.WrapContent, ViewGroup.LayoutParams.WrapContent);
fab.Clickable = true;
fab.SetImageDrawable(Resources.GetDrawable(Resource.Drawable.icon));
SetNativeControl(fab);
}
if (e.NewElement != null)
{
fab.Click += Fab_Click;
}
if (e.OldElement != null)
{
fab.Click -= Fab_Click;
}
}
private void Fab_Click(object sender, EventArgs e)
{
}
}
}
我希望FAB按钮的功能不会出现在iOS UI上。
如果你只希望这个控件中显示的Android平台,您可在例如PCL的页面的XAML这样的代码,例如:
<OnPlatform x:TypeArguments="View">
<OnPlatform.Android>
<local:MyFloatButton WidthRequest="50" HeightRequest="50" HorizontalOptions="Center" />
</OnPlatform.Android>
<OnPlatform.iOS>
<!--use other view here-->
</OnPlatform.iOS>
</OnPlatform>
或者你可以检查在添加此控制后面的代码,检查这个博客:Embedding Native Controls into Xamarin.Forms