如何覆盖/修改 Frame 的 Content 属性以接受 Xamarin.Forms 中的多个视图? [英] How to override/modify the Content property of Frame to accept multiple Views in Xamarin.Forms?

查看:11
本文介绍了如何覆盖/修改 Frame 的 Content 属性以接受 Xamarin.Forms 中的多个视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的 C# 模板代码:

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;
    }
}

我是这样使用它的:

<t:PopupFrame>
   <StackLayout HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
         <t:PopupHeader Text="Copy Deck" />
         <t:PopupEntryHeader Text="New Deck Name" />
                
                More XAML here
   </StackLayout>
</t:PopupFrame>

有什么方法可以编码 PopupFrame 以便 StackLayout 成为它的一部分并获取内容.

Is there some way that I can code PopupFrame so that the StackLayout is part of it and it takes content.

这是我想编码的内容:

<t:PopupFrame>
   <t:PopupHeader Text="Copy Deck" />
   <t:PopupEntryHeader Text="New Deck Name" />
                
       More XAML here

</t:PopupFrame>

推荐答案

如果我是对的,您可以通过将 ContentProperty 属性设置为您的 PopupFrame 类来实现此目的一个本身就是一个集合的属性.这将覆盖 FrameContentProperty ,即 Content 以允许您将多个视图设置为内容,而不是仅设置为默认的一个视图框架...

If i am right, you could achieve this by setting the ContentProperty attribute to your PopupFrame class to a property that is itself a collection. This would override the ContentProperty of Frame which is Content 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.

您可以为您的 PopupFrame 类定义一个 ContentProperty,如下所示:

You could define a ContentProperty for your PopupFrame class, like this:

[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;
    }
}

然后你就可以做你想做的事情了:

Then your are able to do something like what you want:

<?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>

我这边的工作同时显示了 PopupHeaderLabel:

Which on my side works showing both the PopupHeader and the Label:

以下内容取自 Ch 书.Xamarin.Forms 上的 Petzold.

XAML 中使用的每个类都允许将一个属性定义为内容属性(有时也称为类的默认属性).对于此内容属性,不需要属性元素标记,并且开始和结束标记中的任何 XML 内容都会自动分配给此属性.很方便,ContentPage的content属性为ContentStackLayout的content属性为Children,内容Frame 的属性是 Content.

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 is Content, the content property of StackLayout is Children, and the content property of Frame is Content.

这些内容属性已记录在案,但您需要知道在哪里查看.类使用 ContentPropertyAttribute 指定其内容属性.如果此属性附加到类,则它会与类声明一起出现在在线 Xamarin.Forms API 文档中.以下是它在 ContentPage 的文档中的显示方式:

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:

[Xamarin.Forms.ContentProperty("Content")]
public class ContentPage : TemplatedPage

如果你大声说出来,听起来有点多余:Content 属性是 ContentPage 的内容属性."

If you say it aloud, it sounds a bit redundant: "The Content property is the content property of ContentPage."

Frame 类的声明类似:

[Xamarin.Forms.ContentProperty("Content")]
public class Frame : ContentView

StackLayout 没有应用 ContentProperty 属性,但 StackLayout 派生自 Layout,并且 Layout 有一个 ContentProperty 属性:

StackLayout doesn’t have a ContentProperty attribute applied, but StackLayout derives from Layout<View>, and Layout<T> has a ContentProperty attribute:

[Xamarin.Forms.ContentProperty("Children")]
public abstract class Layout<T> : Layout, IViewContainer<T>
where T : View

ContentProperty 属性由从 Layout 派生的类继承,所以 Children 的内容属性StackLayout.

The ContentProperty attribute is inherited by the classes that derive from Layout<T>, so Children is the content property of StackLayout.

这篇关于如何覆盖/修改 Frame 的 Content 属性以接受 Xamarin.Forms 中的多个视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆