1
votes

for some need I have to add a grid into my content page. I tried this :

    public HomePage()
    {
        Grid = new Grid
        {
            RowDefinitions = new RowDefinitionCollection() { new RowDefinition() { Height = GridLength.Star } },
            ColumnDefinitions = new ColumnDefinitionCollection() { new ColumnDefinition() { Width = GridLength.Star } },
            VerticalOptions = LayoutOptions.FillAndExpand,
            HorizontalOptions = LayoutOptions.FillAndExpand,
            ColumnSpacing = 0,
            RowSpacing = 0,
        };
    }

    protected override void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        base.OnPropertyChanged(propertyName);
        if (propertyName == ContentProperty.PropertyName)
        {
            if (!IsGridSet)
            {
                Grid.Children.Add(Content);
                IsGridSet = true;
                Content = Grid;
            }
        }
    }

It seems to work because the page is shown correctly, but none control works, Button, entry everything seems to be deactivate or unreachable, like if one another transparent component was placed on foreground.

1
I found something more. If I do when page is loaded with button action it's work ... So I don't understand why it's not working before.theMouk
What exactly does content hold when you put it inside the Grid?FreakyAli
@FreakyAli it contents another grid.theMouk
Why are you adding UI elements in a ViewModel?iSpain17
it's not in the ViewModel ....theMouk

1 Answers

0
votes

I finally get a solution :smile:

    public HomePage()
    {
        Grid = new Grid
        {
            RowDefinitions = new RowDefinitionCollection() { new RowDefinition() { Height = GridLength.Star } },
            ColumnDefinitions = new ColumnDefinitionCollection() { new ColumnDefinition() { Width = GridLength.Star } },
            VerticalOptions = LayoutOptions.FillAndExpand,
            HorizontalOptions = LayoutOptions.FillAndExpand,
            ColumnSpacing = 0,
            RowSpacing = 0,
        };
    }
public static BindableProperty HomeContentProperty = BindableProperty.Create(nameof(HomeContent), typeof(Xamarin.Forms.View), typeof(HomePage));
public Xamarin.Forms.View HomeContent
{
    get => (Xamarin.Forms.View)GetValue(HomeContentProperty);
    set => SetValue(HomeContentProperty, value);
}

Grid Grid { get; set; }
protected override void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
    base.OnPropertyChanged(propertyName);
    if (HomeContentProperty.PropertyName == propertyName)
    {
        if (HomeContent != null)
        {
            Grid.Children.Add(HomeContent);
            Content = Grid;
        }
    }
}

And I can show my alert from anywhere I want.