如果我是对的,你可以通过设置来实现这一点ContentProperty
归因于你的PopupFrame
类本身就是一个集合的属性。这将覆盖ContentProperty
of Frame
这是Content
允许您将多个视图设置为内容,而不是仅将一个视图设置为 Frame 的默认内容...
因此,如果这一切听起来不错,请继续阅读。
操作方法
你可以定义一个ContentProperty
为您PopupFrame
类,像这样:
[Xamarin.Forms.ContentProperty("Contents")]
class PopupFrame : Frame
{
StackLayout contentStack { get; } = new StackLayout();
public IList<View> Contents { get => contentStack.Children; }
public PopupFrame()
{
Content = contentStack;
HasShadow = true;
HorizontalOptions = LayoutOptions.FillAndExpand;
Padding = 0;
VerticalOptions = LayoutOptions.Center;
}
}
然后你就可以做你想做的事情:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:t="clr-namespace:popupframe"
x:Class="popupframe.MainPage">
<StackLayout>
<t:PopupFrame>
<t:PopupHeader Text="Test header"/>
<Label Text="Test content"/>
</t:PopupFrame>
</StackLayout>
</ContentPage>
我这边的工作显示了PopupHeader
和Label
:
最后是一些关于 ContentProperty 的理论
以下内容逐字取自Ch 的书。 Xamarin.Forms 上的 Petzold https://learn.microsoft.com/en-us/xamarin/xamarin-forms/creating-mobile-apps-xamarin-forms/.
每个类中使用XAML允许将一个属性定义为内容属性(有时也称为类的默认属性)。对于此内容属性,不需要属性元素标记,开始和结束标记内的任何 XML 内容都会自动分配给此属性。非常方便,内容属性ContentPage
is Content
,内容属性StackLayout
is Children
,以及内容属性Frame
is Content
.
这些内容属性已记录在案,但您需要知道在哪里查找。类使用 ContentPropertyAttribute 指定其内容属性。如果此属性附加到类,它将与类声明一起出现在在线 Xamarin.Forms API 文档中。这是它在文档中的显示方式ContentPage
:
[Xamarin.Forms.ContentProperty("Content")]
public class ContentPage : TemplatedPage
如果大声说出来,听起来有点多余:“Content 属性是 ContentPage 的内容属性。”
声明为Frame
类是类似的:
[Xamarin.Forms.ContentProperty("Content")]
public class Frame : ContentView
StackLayout
没有ContentProperty
应用了属性,但是StackLayout
源自于Layout<View>
, and Layout<T>
has a ContentProperty
属性:
[Xamarin.Forms.ContentProperty("Children")]
public abstract class Layout<T> : Layout, IViewContainer<T>
where T : View
The ContentProperty
属性由派生自的类继承Layout<T>
, so Children
是内容属性StackLayout
.