xamarin stacklayout child 删除自身

xamarin stacklayout child removes itself

我有一个简单的 Stacklayout 显示按钮。 我希望能够让 children 从 stacklayout.

中删除自己

此项目仅用于测试目的,因此每个按钮都链接到相同的 event-handler。

private void Button_Pressed_1(object sender, EventArgs e)
{
    stack.Children.RemoveAt(stack.Children.Count - 1);
}

一切正常,直到一个按钮自行删除,然后出现以下不受支持的错误:

未处理的异常:

System.NotSupportedException: Unable to activate instance of type Xamarin.Forms.Platform.Android.AppCompat.ButtonRenderer from native handle 0xbfb79bfc (key_handle 0x3e59524).

有人知道如何实现吗?因为它是一个 nonSupportedException 简单的 try & catch 没有完成任务

编辑: 我让它工作了,我将事件处理程序注册到 Pressed-Event。显然这就是问题所在,当使用 Clicke-Event 时一切正常。

您使用的 Xamarin 是什么版本?我尝试使用 Xamarin.Form 3.0.0.561731 并且效果很好。 请检查我的代码如下:

Xaml 页数:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:RemoveItSelf"
             x:Class="RemoveItSelf.MainPage">

    <StackLayout x:Name="stack">
        <!-- Place new controls here -->
        <Label Text="Welcome to Xamarin.Forms!" 
           HorizontalOptions="Center"
           VerticalOptions="CenterAndExpand" />
        <Label Text="Welcome to Xamarin.Forms!" 
               HorizontalOptions="Center"
               VerticalOptions="CenterAndExpand" />
        <Button Text="Remove" Clicked="PressMeButton_Clicked"></Button>
    </StackLayout>

</ContentPage>

Xaml.cs 页数:

namespace RemoveItSelf
{
    public partial class MainPage : ContentPage
    {
        public MainPage()
        {
            InitializeComponent();
        }

        private void PressMeButton_Clicked(object sender, EventArgs e)
        {
            //stack.Children.RemoveAt(0);
            stack.Children.RemoveAt(stack.Children.Count - 1);
        }
    }
}