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
ContentProperty
attribute to yourPopupFrame
class to a property that is itself a collection. This would override theContentProperty
ofFrame
which isContent
to 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
ContentProperty
for yourPopupFrame
class, like this:Then your are able to do something like what you want:
Which on my side works showing both the
PopupHeader
and 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
ContentPage
isContent
, the content property ofStackLayout
isChildren
, and the content property ofFrame
isContent
.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
Frame
class is similar:StackLayout
doesn’t have aContentProperty
attribute applied, butStackLayout
derives fromLayout<View>
, andLayout<T>
has aContentProperty
attribute:The
ContentProperty
attribute is inherited by the classes that derive fromLayout<T>
, soChildren
is the content property ofStackLayout
.