[英]How to get Button Coordinates for button in iOS Xamarin form
我在下面創建了渲染以獲得坐標。
我正在獲取 X 坐標值,但 Y 坐標值始終為 0。
public class ContextMenuButtonRenderer : ButtonRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Button> e)
{
base.OnElementChanged(e);
if (Element is ContextMenuButton contextButton)
{
contextButton.GetCoordinates = GetCoordinatesNative;
}
}
private (int x, int y) GetCoordinatesNative()
{
return ((int)Frame.Left, (int)Frame.Top);
}
}
如何獲取Y坐標值?
Xamarin.Froms中有一個比較簡單的方法。 AbsoluteLayout
可以指定 position 和子級別。 添加按鈕的時候可以確定按鈕的坐標position,像這樣:
<StackLayout >
<Label Text="Welcome to Xamarin.Forms!" HorizontalTextAlignment="Center" />
<AbsoluteLayout>
<Button Text="111" AbsoluteLayout.LayoutBounds="80,100,50,20"/>
<Button Text="222" AbsoluteLayout.LayoutBounds="180,100,50,20"/>
</AbsoluteLayout>
</StackLayout>
更詳細的可以參考文檔: Xamarin.Forms AbsoluteLayout
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.