반응형
파일구조
MainPage.xaml 작성 해주고
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <?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:App8CustomRenderers;assembly=App8CustomRenderers" x:Class="App8CustomRenderers.MainPage"> <StackLayout VerticalOptions="CenterAndExpand" HorizontalOptions="CenterAndExpand"> <Label Text="Hello, Custom Renderer!" /> <local:MyEntry Text="In Shared Code" /> </StackLayout> </ContentPage> | cs |
Forms에 커스텀 할 Entry 적어줌
1 2 3 4 5 6 7 8 9 10 11 12 | using System; using System.Collections.Generic; using System.Text; using Xamarin.Forms; namespace App8CustomRenderers { public class MyEntry : Entry { } } | cs |
IOS
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | using System; using System.Collections.Generic; using System.Linq; using System.Text; using App8CustomRenderers; using App8CustomRenderers.iOS; using Foundation; using UIKit; using Xamarin.Forms; using Xamarin.Forms.Platform.iOS; [assembly: ExportRenderer(typeof(MyEntry), typeof(MyEntryRenderer))] namespace App8CustomRenderers.iOS { public class MyEntryRenderer : EntryRenderer { protected override void OnElementChanged(ElementChangedEventArgs<Entry> e) { base.OnElementChanged(e); if (Control != null) { // do whatever you want to the UITextField here! Control.BackgroundColor = UIColor.FromRGB(204, 153, 255); Control.BorderStyle = UITextBorderStyle.Line; } } } } | cs |
안드로이드
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | using System; using System.Collections.Generic; using System.Linq; using System.Text; using Android.App; using Android.Content; using Android.OS; using Android.Runtime; using Android.Views; using Android.Widget; using App8CustomRenderers; using App8CustomRenderers.Droid; using Xamarin.Forms; using Xamarin.Forms.Platform.Android; [assembly: ExportRenderer(typeof(MyEntry), typeof(MyEntryRenderer))] namespace App8CustomRenderers.Droid { class MyEntryRenderer : EntryRenderer { public MyEntryRenderer(Context context) : base(context) { } protected override void OnElementChanged(ElementChangedEventArgs<Entry> e) { base.OnElementChanged(e); if (Control != null) { Control.SetBackgroundColor(global::Android.Graphics.Color.LightGreen); } } } } | cs |
각각 플랫폼에 ExportRenderer를 이용하서 각 플렛폼에 해당하는 프로젝트들이 동작한다.
실행
따라한 소스는 https://developer.xamarin.com/samples/xamarin-forms/CustomRenderers/Entry/
'프로그래밍 > xamarin' 카테고리의 다른 글
[xamarin]어플 닫기 전에 물어보고 닫기 (0) | 2018.06.24 |
---|---|
[xamarin]Lottie Animation (0) | 2018.06.10 |
[xamarin]Alert Dialog (0) | 2018.05.27 |
[xamarin] Profile UI Design (0) | 2018.05.20 |
[xamarin]로그인화면 (0) | 2018.05.13 |