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

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):

 

主站蜘蛛池模板: 南岸区| 顺昌县| 社旗县| 德令哈市| 于田县| 红原县| 桑植县| 桦甸市| 雷波县| 闽侯县| 白水县| 新丰县| 江口县| 将乐县| 泸水县| 长岭县| 潮安县| 探索| 孙吴县| 永福县| 凌云县| 仁寿县| 盈江县| 府谷县| 察雅县| 循化| 南靖县| 闽侯县| 册亨县| 大埔县| 冷水江市| 沅江市| 松溪县| 大石桥市| 化隆| 化州市| 松阳县| 黄骅市| 丘北县| 泰安市| 准格尔旗|