简体   繁体   English

xamarin PCL形成共享的自定义控件

[英]xamarin PCL forms shared custom controls

I read a lot of posts about controls customizations . 我阅读了很多关于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. 我的问题非常简单:每个教程帖都谈到在PCL共享项目中创建自定义渲染器,并在具有特定渲染器的每个平台中实现它。

Example: this or this posts on xamarin guide and xamarin blog. 示例: 这个这个帖子在xamarin指南和xamarin博客上。

Is there a way to implement it only once in the PCL shared project and use it everywhere? 有没有办法在PCL共享项目中只实现一次并在任何地方使用它?

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. 它们的目的是将Xamarin.Forms元素转换为本机控件。

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 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 MyControl.xaml.cs

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

Page1.xaml 的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. 但是,如果您想自己绘制所有自定义控件,可以使用skiasharp跨平台绘图库来实现结果。 See this blog post for more details: Cross-Platform 2D Graphics with SkiaSharp 有关更多详细信息,请参阅此博客文章: 使用SkiaSharp的跨平台2D图形

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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