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