MAUI .NET 8 - Xamarin Migrated project

Aneetha 65 Reputation points
2024-07-19T05:31:32.3766667+00:00

I have created a new MAUI .NET 8 project and migrated the Xamarin forms project code.

In my project, I used RelativeLayout for UI design on all the pages, now this RelativeLayout is outdated in MAUI.

I used the Compatability namespace to use RelativeLayout in my MAUI project. However, the RelativeLayout is not working properly in MAUI. the position of elements in RelativeLayout working weirdly.

My question is,

RelativeLayout will work properly in MAUI or not?

Do I need to strictly not use RelativeLayout in MAUI? If yes what is the recommended layout in MAUI which similar/alternative to RelativeLayout?

With Grid Layout, we can place an element with an absolute position? like we do in RelativeLayout.

For example, how do I achieve the below RelativeLayout code in Grid Layout,

 
rtlMain.Children.Add(titleContent,    Microsoft.Maui.Controls.Compatibility.Constraint.RelativeToParent((parent) =>
  {
      return parent.X;
  }), Microsoft.Maui.Controls.Compatibility.Constraint.RelativeToParent((parent) =>
  {
      return parent.Y;
  }), Microsoft.Maui.Controls.Compatibility.Constraint.RelativeToParent((parent) =>
  {
      return parent.Width;
  }), Microsoft.Maui.Controls.Compatibility.Constraint.RelativeToParent((parent) =>
  {

      return (parent.Height > parent.Width ? parent.Height : parent.Width) * .06;

  }));



 
  rtlMain.Children.Add(mainContent, Microsoft.Maui.Controls.Compatibility.Constraint.RelativeToParent((parent) =>
  {
      return parent.X;
  }), Microsoft.Maui.Controls.Compatibility.Constraint.RelativeToParent((parent) =>
  {
      return (parent.Height* 0.20);
  }), Microsoft.Maui.Controls.Compatibility.Constraint.RelativeToParent((parent) =>
  {
      return parent.Width;
  }), Microsoft.Maui.Controls.Compatibility.Constraint.RelativeToParent((parent) =>
  {
      return parent.Height - (parent.Height * .32);
  }));


  rtlMain.Children.Add(footerContent,   Microsoft.Maui.Controls.Compatibility.Constraint.RelativeToParent((parent) =>
  {
      return parent.X;
  }),Microsoft.Maui.Controls.Compatibility. Constraint.RelativeToParent((parent) =>
  {

      return parent.Height - (parent.Height > parent.Width ? parent.Height : parent.Width) * 0.08;
  }), Microsoft.Maui.Controls.Compatibility.Constraint.RelativeToParent((parent) =>
  {
      return parent.Width;
  }), Microsoft.Maui.Controls.Compatibility.Constraint.RelativeToParent((parent) =>
  {
      return (parent.Height > parent.Width ? parent.Height : parent.Width) * 0.08;
  }));

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,578 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 76,386 Reputation points Microsoft Vendor
    2024-07-19T07:40:06.9066667+00:00

    Hello,

    Do I need to strictly not use RelativeLayout in MAUI? If yes what is the recommended layout in MAUI which similar/alternative to RelativeLayout?

    You can use Use Grid instead, from this document:Layout behavior changes from Xamarin.Forms

    Best Regards,

    Leon Lu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    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.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.