我一直在尝试在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 (); } }更多推荐
发布评论