单击按钮Xamarin.forms将一个孩子添加到父项 [英] Add a Child to the Parent on Button Click Xamarin.forms

查看:76
本文介绍了单击按钮Xamarin.forms将一个孩子添加到父项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试在Android中将Label视图添加到Button Click上的Stacklayout中. 但是它会引发Null Pointer异常.以下是我要达到的目标.任何人都可以在xamarin.forms中获得如何实现此目标的建议

I have been trying to add a Label view to the Stacklayout on Button Click in Android. But It throws Null Pointer exception. Below is what I'm trying to acheive. Can anyone please advice on how to acheive this in xamarin.forms

Ca中的Xamarin.Forms代码

 StackLayout parent= new StackLayout ();

 Button add= new Button
        {
            HorizontalOptions=LayoutOptions.End,
            BackgroundColor=Xamarin.Forms.Color.White,
            Text="ADD",
            TextColor=Xamarin.Forms.Color.Maroon,
        };

 add.Clicked += OnButtonClicked;

 Label firstLabel = new Label
        {
            Text = "Label 1",
            HorizontalOptions = LayoutOptions.StartAndExpand,
            TextColor=Xamarin.Forms.Color.FromHex("#000000")
        };
 parent.Children.Add(add);
 parent.Children.Add(firstLabel );

在ButtonClick中添加标签

 void OnButtonClicked(object sender, EventArgs e)
 {

   Label secondLabel = new Label
        {
            Text = "Label 1",
            HorizontalOptions = LayoutOptions.StartAndExpand,
            TextColor=Xamarin.Forms.Color.FromHex("#000000")
        };
  parent.Children.Add(secondLabel ); 
}

预先感谢

推荐答案

您的代码按原样工作...做了一点微小的更改-将parent设为类字段,以便在OnButtonClicked

Your code works as is... with one tiny change - make parent a class field so it's referenced from within the OnButtonClicked

确保您更新了解决方案包,以便拥有最新的Xamarin.Forms.始终在解决方案级别上更新软件包,以免发生版本冲突

Make sure you update the solution packages so you have the latest Xamarin.Forms. Always update the packages on the solution level so do don't get versioning conflicts

此版本已通过测试,可在iOS上运行:

This version was tested and works on iOS:

public class LabelPage: ContentPage
    {
        StackLayout parent = null;

        public LabelPage ()
        {
            parent = new StackLayout ();

            Button add = new Button {
                HorizontalOptions = LayoutOptions.End,
                BackgroundColor = Xamarin.Forms.Color.White,
                Text = "ADD",
                TextColor = Xamarin.Forms.Color.Maroon,
            };

            add.Clicked += OnButtonClicked;

            Label firstLabel = new Label {
                Text = "Label 1",
                HorizontalOptions = LayoutOptions.StartAndExpand,
                TextColor = Xamarin.Forms.Color.FromHex ("#000000")
            };
            parent.Children.Add (add);
            parent.Children.Add (firstLabel); 

            Content = parent;
        }

        void OnButtonClicked (object sender, EventArgs e)
        { 
            Label secondLabel = new Label {
                Text = "Label 1",
                HorizontalOptions = LayoutOptions.StartAndExpand,
                TextColor = Xamarin.Forms.Color.FromHex ("#000000")
            };
            parent.Children.Add (secondLabel); 
            //UpdateChildrenLayout ();
        }
    }

这篇关于单击按钮Xamarin.forms将一个孩子添加到父项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆