Xamarin.forms 如何在xamarin表单xaml中实例化xamarin android/ios本机自定义控件类
自定义控件类:Xamarin.forms 如何在xamarin表单xaml中实例化xamarin android/ios本机自定义控件类,xamarin.forms,Xamarin.forms,自定义控件类: public class CustomTextInput : UITextField { } <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:Custom="clr-namespace:SampleApp.C
public class CustomTextInput : UITextField
{
}
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:Custom="clr-namespace:SampleApp.CustomControls;assembly=SampleApp"
x:Name="SampleAppView">
<ContentPage.Content>
<StackLayout Orientation="Horizontal">
<Image Source="Sample_ic.png" WidthRequest="29" HeightRequest="29"/>
<StackLayout Orientation="Vertical">
<Label Text="Sample Text" TextColor="#717073" FontSize="Small" FontFamily="Helvetica"/>
<!--Accessing IOS Custom control class-->
<Custom:CustomTextInput Text="50 Gal" BackgroundColor="Transparent" TextColor="#838288" />
</StackLayout>
</StackLayout>
</ContentPage.Content>
</ContentPage>
Xamarin.Forms-xaml:
public class CustomTextInput : UITextField
{
}
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:Custom="clr-namespace:SampleApp.CustomControls;assembly=SampleApp"
x:Name="SampleAppView">
<ContentPage.Content>
<StackLayout Orientation="Horizontal">
<Image Source="Sample_ic.png" WidthRequest="29" HeightRequest="29"/>
<StackLayout Orientation="Vertical">
<Label Text="Sample Text" TextColor="#717073" FontSize="Small" FontFamily="Helvetica"/>
<!--Accessing IOS Custom control class-->
<Custom:CustomTextInput Text="50 Gal" BackgroundColor="Transparent" TextColor="#838288" />
</StackLayout>
</StackLayout>
</ContentPage.Content>
</ContentPage>
我可以访问xamarin.forms xaml中的android/IOS自定义控件类吗?对于示例,我使用UITextField控件。但我需要访问特定于android/IOS平台的自定义控件,这在xamarin.forms中是不可用的 如果您使用的是Xamarin.Forms,那么您使用的是独立于平台的控件
UITextField
不存在,而是映射到条目
您最好的选择是创建一个自定义条目子类:
public class MyEntry : Entry
{
}
并在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"
x:Class="MyApp.SomePage"
xmlns:local="clr-namespace:MyApp;assembly=MyApp">
<local:MyEntry />
</ContentPage>
然后,在每个平台上,您可以提供特定于平台的
[程序集:导出呈现程序(typeof(MyApp.MyEntry)、typeof(MyApp.iOS.MyEntryRenderer))]
名称空间MyApp.iOS
{
公共类MyEntryRenderer:EntryRenderer
{
受保护的覆盖无效OnElementChanged(ElementChangedEventArgs e)
{
碱基。一个元素改变(e);
if(null==控件)
回来
var textField=控件为UITextField;
//用textField做一些事情
}
}
}
如果您发现单独呈现自定义控件无法提供所需的确切控件级别,您还可以创建自定义的
页面呈现器,如图所示。Xamarin.Forms会将其控件(如Entry
)呈现到每个特定平台的本机控件中,那么您的问题是什么?也许您正在Xamarin.Forms中寻找类似smth的自定义呈现器?不。我需要呈现Xamarin.Forms中不可用的android/ios特定控件。我认为您不理解Xamarin.Forms的概念,请阅读它的工作原理以及为什么您不应该关注Xamarin.Forms中的本机控件。