Aryan M
Aryan M

Reputation: 657

Not able to link the xamarin forms custom control in the content page

I'm not able to link the xamarin forms custom control in the content page.

Have created the following xamarin custom control "AlertMessage",

public class AlertMessage:ContentView
{
    private Frame _frame;
    private Grid _alertGrid;
    private StackLayout _alertLayout, _alertLayoutContent;
    private BoxView _alertBoxView;
    private Label _alertMessage;

    public static readonly BindableProperty TitleProperty = BindableProperty.Create("Title", typeof(string), typeof(AlertMessage), default(string));

    public string Title
    {
        get { return (string)GetValue(TitleProperty); }
        set { SetValue(TitleProperty, value); }
    }

    public static readonly BindableProperty TextProperty = BindableProperty.Create("Text", typeof(string), typeof(AlertMessage), default(string));

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

    public AlertMessage()
    {
        InitLayout();
    }

    public void InitLayout()
    {
        _alertGrid = new Grid { Padding = 0 };

        _alertGrid.RowDefinitions.Add(new RowDefinition
        {
            Height = GridLength.Star
        });

        _alertGrid.ColumnDefinitions.Add(new ColumnDefinition { Width = 8 });
        _alertGrid.ColumnDefinitions.Add(new ColumnDefinition { Width = GridLength.Star });

        _alertLayout = new StackLayout
        {
            HorizontalOptions = LayoutOptions.FillAndExpand,
            VerticalOptions = LayoutOptions.FillAndExpand,
            BackgroundColor = Constants.MMSGAlertBackgroundColor,
            Padding = 0
        };

        _alertBoxView = new BoxView
        {
            Color = Constants.MMSGAlertTextColor,
            VerticalOptions = LayoutOptions.FillAndExpand
        };

        _alertLayout.Children.Add(_alertBoxView);

        _alertLayoutContent = new StackLayout
        {
            HorizontalOptions = LayoutOptions.FillAndExpand,
            VerticalOptions = LayoutOptions.FillAndExpand,
            Padding = 16
        };

        _alertMessage = new Label
        {
            Text = Text,
            TextColor = Constants.MMSGAlertTextColor
        };

        _alertLayoutContent.Children.Add(_alertMessage);

        _alertGrid.Children.Add(_alertLayout, 0, 0);
        _alertGrid.Children.Add(_alertLayoutContent, 1, 0);

        _frame = new Frame
        {
            OutlineColor = Constants.MMSGAlertBorderColor,
            BackgroundColor = Constants.MMSGAlertBackgroundColor,
            Padding = new Thickness(2, 2, 0, 2),
            HasShadow = false,
            Content = _alertGrid,
            VerticalOptions = LayoutOptions.FillAndExpand
        };

        this.Content = _frame;
    }
}

I'm trying to render this custom control in the content page using the c# code,

var alertMessage = new AlertMessage
            {
                Text = ViewModel.AlertReviewMessage,
                Title = "Please review"
            };

I'm getting the following build error, on rendering this custom control in the content page.

Severity    Code    Description Project File    Line    Suppression State
Error       Can't resolve the reference 'APP.Mobile.CustomControls.AlertText', referenced from the method 'System.Object APP.Mobile.StatusSummaryContentPage::<GetDataTemplate>b__7_0()' in 'MMSG.Mobile, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.   RemServ.Mobile.iOS

Please let me know what link am i missing here to add a custom control to a content page

Upvotes: 0

Views: 50

Answers (1)

Morse
Morse

Reputation: 9115

Your content view is missing InitializeComponent(); in the constructor.It should be like below

public AlertView()
{
    InitializeComponent();
    Content.BindingContext = this;
}

I hope you have added reference to the shared project from custom controls project. using APP.Mobile.CustomControls;

Also you should add [XamlCompilation(XamlCompilationOptions.Compile)] on top of the class declaration.

Upvotes: 1

Related Questions