Xamarin.Forms ile Custom Controls Geliştirme

Xamarin.Forms ile Custom Controls Geliştirme

Xamarin.Forms ile Custom Controls Geliştirme

Xamarin.Forms, yerleşik kontrollerle birçok ihtiyacı karşılar. Ancak, özel tasarım ve işlevsellik gerektiren durumlarda kendi kontrollerinizi oluşturabilirsiniz. Bu makalede özel bir kontrol oluşturmanın yollarını inceleyeceğiz.

Custom Control Nedir?

Custom Control, standart kontrolleri genişleterek veya sıfırdan bir kontrol oluşturarak özel bir kullanıcı arayüzü ve işlevsellik sağlamaktır.

Örnek: Özel Bir Button Kontrolü

Aşağıdaki örnekte, bir simge ve metin içeren özel bir buton kontrolü oluşturacağız.

Custom Control Sınıfı


public class IconButton : StackLayout
{
    public Image Icon { get; set; }
    public Label TextLabel { get; set; }

    public IconButton()
    {
        Icon = new Image { HeightRequest = 24, WidthRequest = 24 };
        TextLabel = new Label { FontSize = 16, VerticalTextAlignment = TextAlignment.Center };

        this.Orientation = StackOrientation.Horizontal;
        this.Children.Add(Icon);
        this.Children.Add(TextLabel);
    }

    public static readonly BindableProperty TextProperty =
        BindableProperty.Create(nameof(Text), typeof(string), typeof(IconButton), default(string),
            propertyChanged: (bindable, oldValue, newValue) =>
            {
                var control = (IconButton)bindable;
                control.TextLabel.Text = (string)newValue;
            });

    public string Text
    {
        get => (string)GetValue(TextProperty);
        set => SetValue(TextProperty, value);
    }

    public static readonly BindableProperty IconSourceProperty =
        BindableProperty.Create(nameof(IconSource), typeof(string), typeof(IconButton), default(string),
            propertyChanged: (bindable, oldValue, newValue) =>
            {
                var control = (IconButton)bindable;
                control.Icon.Source = (string)newValue;
            });

    public string IconSource
    {
        get => (string)GetValue(IconSourceProperty);
        set => SetValue(IconSourceProperty, value);
    }
}

Kullanım



Özelleştirme ve Geliştirme

Custom Control’leri genişleterek temalar, animasyonlar veya ek özellikler ekleyebilirsiniz. Ayrıca, platforma özgü görünümleri desteklemek için Renderer'lar kullanabilirsiniz.

Sonuç

Custom Controls, uygulamanızı farklılaştırmak ve kullanıcı deneyimini geliştirmek için güçlü bir araçtır. Xamarin.Forms ile bu kontrolleri kolayca geliştirebilirsiniz.