c# - xamarin PCL 表单共享自定义控件

标签 c# xamarin xamarin.forms custom-renderer

我阅读了很多关于控件自定义的文章。我的问题很简单:每个教程帖子都谈到在您的 PCL 共享项目中创建自定义渲染器,并在每个平台上使用特定渲染器实现它。

示例:thisthis xamarin 指南和 xamarin 博客上的帖子。

有没有办法在PCL共享项目中只实现一次,到处使用?

我的目标是创建一个可能是全新的自定义控件,例如包含一些矩形和一些标签的控件。或者你能想象到的任何东西。我无法实现的唯一功能是仅在共享项目中实现。

谢谢大家,如有任何回复将不胜感激

最佳答案

自定义渲染器是特定于平台的。它们的目的是将 Xamarin.Forms 元素转换为 native 控件。

您的用例听起来像是一个复合控件。您可以做的是使用所有可用控件包装到可重用组件中。

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

你可以在你的页面中使用它。

<?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>

关于c# - xamarin PCL 表单共享自定义控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36788540/

相关文章:

c# - BindingInflate 与不同的 ViewModel

android - 聚焦字段时不会触发 Xamarin Forms BACK BUTTON

c# - 如果我在子类中实现 ISerializable,父类是否也必须如此?

c# - 检查字符串中的字符是 1 还是 0

javascript - 如何在 View 中访问我的 C# 数组?

xamarin - 如何在 Xamarin MAC 平台中更改选取器背景颜色

c# - 如何从 Xamarin Forms 项目中调用 Android 和 iOS 项目中可用的方法?

c# - 您可以 [过时] Razor 中的 DisplayTemplate 吗?

Xamarin Forms Android 错误 : Please install package 'Android Support Library'

xamarin 自动布局不起作用 - 它在完全约束时不遵守约束