Forum Xamarin.Forms
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

Xamarin Custom Control - How to?

I followed this article to create a basic "custom control" for Xamarin... No joy!
https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/layouts/contentview

Using this article I simplified the whole thing... I need a very simple control for a mobile reporting app. The current app has LETTER designations (so a Label), in a colored circle (each color means a phase for that designation. Essentially this can be RED, YELLOW, or GREEN... go figure!

I designed a XAML page with a FRAME, and within it is a LABEL... I bound it as is in the article... I set the widthrequest and heightrequest to twice the CornerRadius (i.e a circle)... This control will be databound in a FLEXVIEW ItemSource... This should be easy as heck to accomplish... So far I get no errors, but nothing is rendered on screen...

Xamarin.Essentials 1.5.3.2
Xamarin.Forms 4.6.0.800
.NET Standard Library 2.0.3

All the code is in the article... Here is the XAML

    <Frame BindingContext="{x:Reference this}" WidthRequest="42" HeightRequest="42" HasShadow="False" Padding="0,2" Margin="8" VerticalOptions="Center" HorizontalOptions="Center" BackgroundColor="Transparent" BorderColor="Black" CornerRadius="21">
        <Label Text="{Binding Text, FallbackValue='1'}" HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand" FontSize="23" FontAttributes="Bold, Italic" />
    </Frame>

public partial class StatusItemControl : ContentView
{
    public static readonly BindableProperty TextProperty = BindableProperty.Create("Text",          // the name of the bindable property
                                                                                typeof(string),         // the bindable property type
                                                                                typeof(StatusItemControl),// the parent object type
                                                                                string.Empty);          // the default value for the property
    public string Text
    {
        get => (string)GetValue(StatusItemControl.TextProperty);
        set => SetValue(StatusItemControl.TextProperty, value);
    }

    public DrawItemControl()
    {
        InitializeComponent();

    }
}

Answers

  • LandLuLandLu Member, Xamarin Team Xamurai

    I tested your code on my side, however, it works properly:

    I bind a text on it when using on a page:

    <local:StatusItemControl Text="{Binding Text}"/>
    

    How did you consume this custom control?

  • StewartBasterashStewartBasterash USMember ✭✭

    Here is a question? What is the result if you use static text... Like;

  • LandLuLandLu Member, Xamarin Team Xamurai

    Binding for static stuff is different.
    We need to use x:Static.
    Refer to my another thread for more details:
    https://forums.xamarin.com/discussion/comment/412210#Comment_412210

Sign In or Register to comment.