简体   繁体   中英

xamarin PCL forms shared custom controls

I read a lot of posts about controls customizations . My question is pretty simple: every tutorial-post talks about the creation of a custom renderer in your PCL shared project, and implementing it in every platform with the specific renderer.

Example: this or this posts on xamarin guide and xamarin blog.

Is there a way to implement it only once in the PCL shared project and use it everywhere?

My goal is to create a custom control that might be new at all, for example a control containing some rectangles and some labels. or anything you can imagine. The only feature i can't achieve is the implementation in shared project only.

Thanks all, any reply will be appreciated

Custom renderers are platform specific. They have the purpose to translate the Xamarin.Forms elements to the native control.

Your use case sounds like a compound control. What you can do, is to use all available controls an wrap in into a reusable component.

MyControl.xaml

<?xml version="1.0" encoding="utf-8" ?>
<StackLayout xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="App3.MyControl">
    <Label Text="Hello World" VerticalOptions="Center" HorizontalOptions="Center" />
    <BoxView BackgroundColor="Fuchsia"></BoxView>
    <!-- add whatever you want -->
</StackLayout>

MyControl.xaml.cs

public partial class MyControl : StackLayout
{
    public MyControl()
    {
        InitializeComponent();
    }
}

Page1.xaml

Than you can use it in your pages.

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:App3;assembly=App3"
             x:Class="App3.Page1">
    <local:MyControl></local:MyControl>
</ContentPage>

You need custom renderer for different platform so that you can access native widgets provided by the platform.

However if you want to draw all of the custom control yourself you can use skiasharp cross platform drawing library to achieve the result. See this blog post for more details: Cross-Platform 2D Graphics with SkiaSharp

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM