官术网_书友最值得收藏!

Native components

In some cases, developers need to resort to using native user controls; especially when a certain control only exists for a certain platform (that is, no Xamarin.Forms abstraction exists for that specific UI element). In these types of situations, Xamarin enables users to declare native views within Xamarin.Forms XAML and set/bind the properties of these controls.

In order to include native views, first the namespaces for the native views should be declared:

xmlns:ios="clr-namespace:UIKit;assembly=Xamarin.iOS;targetPlatform=iOS"
xmlns:androidWidget="clr-namespace:Android.Widget;assembly=Mono.Android;targetPlatform=Android"
xmlns:formsandroid="clr-namespace:Xamarin.Forms;assembly=Xamarin.Forms.Platform.Android;targetPlatform=Android"

Once the namespace is declared, we can, for instance, replace Label in our ItemView.xaml and use its native counterpart directly:

<!-- <Label Text="{Binding Description}" /> -->
<ios:UILabel Text="{Binding Description}" View.HorizontalOptions="Start"/>
<androidWidget:TextView Text="{Binding Description}" x:Arguments="{x:Static formsandroid:Forms.Context}" />

Now the view will include a different native control for each platform. Additionally, the UILabel.Text and TextView.Text properties now carry the binding to the Description field. 

It is important to note that, for native view references to work, the view in question should not be included in XamlCompilation. In other words, the view should carry the  [XamlCompilation(XamlCompilationOptions.Skip)] attribute.

It is also possible to further customize the native fields using native types and properties. For instance, in order to add a drop-shadow on the UILabel item, we can use the ShadowColor and ShadowOffset values:

<ios:UILabel 
Text="{Binding Description}"
View.HorizontalOptions="Start"
ShadowColor="{x:Static ios:UIColor.Gray}">
<ios:UILabel.ShadowOffset>
<iosGraphics:CGSize>
<x:Arguments>
<x:Single>1</x:Single>
<x:Single>2</x:Single>
</x:Arguments>
</iosGraphics:CGSize>
</ios:UILabel.ShadowOffset>
</ios:UILabel>

The outcome of this declaration is as follows (compare this to the Xamarin.Forms Label field defined earlier):

 

主站蜘蛛池模板: 陇川县| 贵港市| 浏阳市| 唐山市| 科尔| 抚州市| 喜德县| 甘谷县| 都兰县| 慈利县| 元江| 奉化市| 青海省| 乳山市| 福鼎市| 三江| 镇巴县| 宣武区| 平阳县| 昔阳县| 横山县| 罗城| 武威市| 上饶县| 昌平区| 溆浦县| 衡阳县| 望城县| 临颍县| 华安县| 通化县| 乌拉特中旗| 观塘区| 孝感市| 南投市| 关岭| 龙泉市| 凯里市| 长丰县| 安康市| 吉木乃县|