简体   繁体   English

xamarin stacklayout孩子自己移除

[英]xamarin stacklayout child removes itself

I have a simple Stacklayout that shows Buttons. 我有一个简单的Stacklayout显示按钮。 I want to be able to let children remove itself from the stacklayout . 我希望能够让孩子们从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). System.NotSupportedException:无法从本机句柄0xbfb79bfc(key_handle 0x3e59524)激活Xamarin.Forms.Platform.Android.AppCompat.ButtonRenderer类型的实例。

Has anyone an idea how to accomplish this? 有谁知道如何实现这一目标? Since it's a nonSupportedException a simple try & catch didn't do the job 由于它是一个nonSupportedException一个简单的try&catch没有完成这项工作

EDIT: I got it working, i registered the eventhandler to the Pressed-Event. 编辑:我让它工作,我将事件处理程序注册到Pressed-Event。 Appearently that was the problem, when using the Clicke-Event everything works just fine. 显然这是问题,当使用Clicke-Event时一切正常。

What 's version of Xamarin which you are using? 您使用的Xamarin是什么版本的? I tried with Xamarin.Form 3.0.0.561731 and it worked well. 我尝试使用Xamarin.Form 3.0.0.561731并且效果很好。 Please check my code as bellow: 请检查我的代码如下:

Xaml page: 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 page: 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);
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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