question

PatrickRyder avatar image
0 Votes"
PatrickRyder asked JarvanZhang-MSFT edited

How do I use declarative markup for a RelativeLayout

I use plain C# code and Xamarin.CommunityToolkit.Markup for my UI layout. I am trying to embrace declarative layout but struggle sometimes with the required syntax. My latest difficulty is with a RelativeLayout.

This works okay...

         RelativeLayout layout = new RelativeLayout() { HorizontalOptions = LayoutOptions.FillAndExpand };
         layout.Children.Add
         (
             new Grid(),
             Constraint.Constant( 0.0 ),
             Constraint.Constant( 0.0 ),
             Constraint.RelativeToParent( ( r ) => r.Width ),
             Constraint.RelativeToParent( ( r ) => r.Height )
         );

... but is obviously not entirely declarative.

The closest I have got so far is...

         LayoutOuter = new RelativeLayout()
         {
             HorizontalOptions = LayoutOptions.FillAndExpand,
             Children =
             {
                 LayoutInner,
                 Constraint.Constant( 0.0 ),
                 Constraint.Constant( 0.0 ),
                 Constraint.RelativeToParent( ( r ) => r.Width ),
                 Constraint.RelativeToParent( ( r ) => r.Height )
             }
         };

...but I get an error along the lines of "The call is ambiguous between RelativeLayout.IRelativeList<T>.Add( T, Expression<Func<double>>, Expression<Func<double>>, Expression<Func<double>>, Expression<Func<double>>) and RelativeLayout.IRelativeList<T>.Add( T, Constraint, Constraint, Constraint, Constraint )"

How do I switch to fully declarative syntax?



dotnet-xamarin
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

JarvanZhang-MSFT avatar image
1 Vote"
JarvanZhang-MSFT answered JarvanZhang-MSFT edited

Hello,​

Welcome to our Microsoft Q&A platform!

but I get an error along the lines of "The call is ambiguous between RelativeLayout.IRelativeList ...

We cannot specify a value for RelativeLayout.Children property because it only provides the get function, which works like a read only parameter.

116549-screenshot-2021-07-21-103919.png

Please use RelativeLayout.Children.Add method to add the views to the layout instead.

Check the doc: https://docs.microsoft.com/en-us/dotnet/api/xamarin.forms.relativelayout.children?view=xamarin-forms

Best Regards,

Jarvan Zhang


If the response is helpful, please click "Accept Answer" and upvote it.

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.



5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.