简体   繁体   中英

xamarin stacklayout child removes itself

I have a simple Stacklayout that shows Buttons. I want to be able to let children remove itself from the stacklayout .

this project is just for testing purposes, so every button is linked to the same event-handler.

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

everything's fine until one button removes itself, then the following unsupported error appears:

Unhandled Exception:

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

Has anyone an idea how to accomplish this? Since it's a nonSupportedException a simple try & catch didn't do the job

EDIT: I got it working, i registered the eventhandler to the Pressed-Event. Appearently that was the problem, when using the Clicke-Event everything works just fine.

What 's version of Xamarin which you are using? I tried with Xamarin.Form 3.0.0.561731 and it worked well. Please check my code as bellow:

Xaml page:

<?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 page:

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);
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM