Can I combine the creation of an element and adding a new element together in Xamarin.Forms?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Can I combine the creation of an element and adding a new element together in Xamarin.Forms?



I have this code:


var layout = new StackLayout

Orientation = StackOrientation.Vertical,
    Spacing = 0
;
layout.Children.Add(new HeaderTemplate

    Header = Settings.cc.LongText().ToUpper()
);



Can these two statements be combined in some way?





Not possible, you will need an instance before being able to add children. There is no support in C# at this time to do that from the initialization.
– Gerald Versluis
Aug 13 at 8:22





@GeraldVersluis ? Children is an IList<View>, so yes you can add a comma separated list of children in StackLayout constructor via the initializer shorthand. Now I have no idea what a HeaderTemplate custom(?) class is (ListView has one), so assuming it is a View subclass, that would work fine. )
– SushiHangover
Aug 13 at 9:52



Children


IList<View>


HeaderTemplate





@SushiHangover my mistake, you are right. As pointed out by Limbani Akashs' answer. That is the way to do it :)
– Gerald Versluis
Aug 13 at 9:54





@GeraldVersluis Totally missed Limbani's answer ;-) Guess I did not scroll down far enough, yes, that is it...
– SushiHangover
Aug 13 at 10:01





1 Answer
1



Try this.


var layout = new StackLayout

Orientation = StackOrientation.Vertical,
Spacing = 0,
Children =

new HeaderTemplate

Header = Settings.cc.LongText().ToUpper()


;






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

Creating a leaderboard in HTML/JS