【问题标题】:Add a Child to the Parent on Button Click Xamarin.forms在按钮上向父级添加子级单击 Xamarin.forms
【发布时间】:2014-09-15 18:54:51
【问题描述】:

我一直在尝试将标签视图添加到 Android 中的按钮单击时的 Stacklayout。 但它抛出空指针异常。以下是我想要实现的目标。任何人都可以就如何在 xamarin.forms 中实现这一点提出建议

C# 中的 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 ); 
}

提前致谢

【问题讨论】:

  • 我看不出有什么失败的理由。你能用你得到的堆栈跟踪编辑你的帖子吗?

标签: c# android xamarin xamarin.forms


【解决方案1】:

您的代码按原样运行...只需稍作改动 - 将 parent 设为类字段,以便在 OnButtonClicked 中引用它

确保更新解决方案包,以便拥有最新的 Xamarin.Forms。始终在解决方案级别更新软件包,以免出现版本冲突

此版本已经过测试,可在 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 ();
        }
    }

【讨论】:

  • In parent.Children.Add (secondLabel); 对象引用未设置为对象错误
  • 当您在该行上放置一个断点时,哪个对象为空?父母?
  • 两者都不为空。但它在 Android 中运行时会抛出一个空指针。我不确定IOS。
  • parent.Children 呢?那是空的吗?您所看到的听起来不正确-您是否复制了我发布的确切课程?创建一个新类,复制这个确切的内容,完全不做任何修改,然后让 App.cs 作为根返回。这个问题很可能是由于一些小细节,一旦你看到它就会让你打额头
  • 对不起..我刚刚清理并创建了一个可以工作的新项目.. xamarin 的奇怪行为但是谢谢..
猜你喜欢
  • 1970-01-01
  • 2018-10-01
  • 2021-05-09
  • 1970-01-01
  • 2020-02-23
  • 1970-01-01
  • 2017-01-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多