在按钮上向父级添加子级单击Xamarin.forms(Add a Child to the Parent on Button Click Xamarin.forms)

编程入门 行业动态 更新时间:2024-10-23 13:31:18
在按钮上向父级添加子级单击Xamarin.forms(Add a Child to the Parent on Button Click Xamarin.forms)

我一直在尝试在Android上的Button Click上向Stacklayout添加Label视图。 但它会抛出空指针异常。 以下是我想要实现的目标。 任何人都可以请求如何在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 ); }

提前致谢

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

Xamarin.Forms Code in C#

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

Adding Label in 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 ); }

Thanks in Advance

最满意答案

您的代码按原样工作......只需进行一次微小的更改 - 使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 (); } }

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

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

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

更多推荐

本文发布于:2023-08-05 18:22:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1435737.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:单击   上向   按钮   Xamarin   forms

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!