Created
December 15, 2020 13:28
-
-
Save ryupold/7728caabfde91caebf55853186ba69a5 to your computer and use it in GitHub Desktop.
Xamarin Forms Get Element bounds for iPad share dialoge
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using Xamarin.Forms; | |
public static class Utils | |
{ | |
public static Rectangle GetAbsoluteBounds(this Xamarin.Forms.View element) | |
{ | |
Element looper = element; | |
var absoluteX = element.X + element.Margin.Top; | |
var absoluteY = element.Y + element.Margin.Left; | |
// Add logic to handle titles, headers, or other non-view bars | |
while (looper.Parent != null) | |
{ | |
looper = looper.Parent; | |
if (looper is Xamarin.Forms.View v) | |
{ | |
absoluteX += v.X + v.Margin.Top; | |
absoluteY += v.Y + v.Margin.Left; | |
} | |
} | |
return new Rectangle(absoluteX, absoluteY, element.Width, element.Height); | |
} | |
public static System.Drawing.Rectangle ToSystemRectangle(this Rectangle rect) => | |
new System.Drawing.Rectangle((int) rect.X, (int) rect.Y, (int) rect.Width, (int) rect.Height); | |
public static System.Drawing.Rectangle GetOuterBounds(this View element) => | |
element.GetAbsoluteBounds().ToSystemRectangle(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment