单击按钮时如何获取包含按钮的堆栈布局?

How to get the stacklayout that contains a button when the button is clicked?

我正在使用包含一些其他元素和一个按钮的堆栈布局。

<StackLayout x:Name="layout">
   <Button Text="Button" Clicked="Action"/>
<StackLayout/>

有没有办法在单击按钮时获取包含该按钮的 StackLayout?

使用父级 属性

void Action(object sender, EventArgs args)
{
     var button = sender as Button;
     var stackLayout = button.Parent as StackLayout;
     stackLayout.Children.Remove(button); 
}