gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 23:17:25 24 4
gpt4 key购买 nike

我阅读了很多关于控件自定义的文章。我的问题很简单:每个教程帖子都谈到在您的 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com