Here's the C# template code I have:
public class PopupFrame : Frame
{
public PopupFrame()
{
this.SetDynamicResource(BackgroundColorProperty, "PopUpBackgroundColor");
this.SetDynamicResource(CornerRadiusProperty, "PopupCornerRadius");
HasShadow = true;
HorizontalOptions = LayoutOptions.FillAndExpand;
Padding = 0;
VerticalOptions = LayoutOptions.Center;
}
}
I am using it like this:
<t:PopupFrame>
<StackLayout HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
<t:PopupHeader Text="Copy Deck" />
<t:PopupEntryHeader Text="New Deck Name" />
More XAML here
</StackLayout>
</t:PopupFrame>
Is there some way that I can code PopupFrame so that the StackLayout is part of it and it takes content.
Here's what I would like to code:
<t:PopupFrame>
<t:PopupHeader Text="Copy Deck" />
<t:PopupEntryHeader Text="New Deck Name" />
More XAML here
</t:PopupFrame>
If i am right, you could achieve this by setting the
ContentPropertyattribute to yourPopupFrameclass to a property that is itself a collection. This would override theContentPropertyofFramewhich isContentto allow you to set multiple views as the contents instead of just one which is the default for Frame...So, if all this sounds good to you, keep on reading.
The HowTo
You could define a
ContentPropertyfor yourPopupFrameclass, like this:Then your are able to do something like what you want:
Which on my side works showing both the
PopupHeaderand theLabel:And finally a bit of theory on ContentProperty
What follows is taken literally from the book of Ch. Petzold on Xamarin.Forms.
Every class used in XAML is allowed to define one property as a content property (sometimes also called the class’s default property). For this content property, the property-element tags are not required, and any XML content within the start and end tags is automatically assigned to this property. Very conveniently, the content property of
ContentPageisContent, the content property ofStackLayoutisChildren, and the content property ofFrameisContent.These content properties are documented, but you need to know where to look. A class specifies its content property by using the ContentPropertyAttribute. If this attribute is attached to a class, it appears in the online Xamarin.Forms API documentation along with the class declaration. Here’s how it appears in the documentation for
ContentPage:If you say it aloud, it sounds a bit redundant: "The Content property is the content property of ContentPage."
The declaration for the
Frameclass is similar:StackLayoutdoesn’t have aContentPropertyattribute applied, butStackLayoutderives fromLayout<View>, andLayout<T>has aContentPropertyattribute:The
ContentPropertyattribute is inherited by the classes that derive fromLayout<T>, soChildrenis the content property ofStackLayout.