-1

我有 XAML 代码,用于设置这样的框架和框架标题:

<Label Text="ABC" />
<t:ContentFrame>
   <Label Text="ABC" />
</t:ContentFrame>

因为ContentFrame我在这里使用 C# 模板:

[Xamarin.Forms.ContentProperty("Contents")]
public class ContentFrame : CustomFrame
{
    StackLayout contentStack { get; } = new StackLayout()
    {
        Spacing = 0,
        Padding = new Thickness(0),
        Orientation = StackOrientation.Vertical
    };
    public IList<View> Contents { get => contentStack.Children; }

    public ContentFrame()
    {
        Content = contentStack;
        HasShadow = false;
        SetDynamicResource(BackgroundColorProperty, "ContentFrameBackgroundColor");
        SetDynamicResource(BorderColorProperty, "ContentFrameBorderColor");
        SetDynamicResource(CornerRadiusProperty, "ContentFrameCornerRadius");
    }
}

有没有一种方法可以将标题的设置组合到ContentFrame类中,以便可以通过以下方式实现相同的目标:

<t:ContentFrame Heading="ABC">
   <Label Text="ABC" />
</t:ContentFrame>
4

1 回答 1

0

为此,您需要一个Bindable 属性

public class ContentFrame : CustomFrame
{
StackLayout contentStack { get; } = new StackLayout()
    {
        Spacing = 0,
        Padding = new Thickness(0),
        Orientation = StackOrientation.Vertical
    };
    public IList<View> Contents { get => contentStack.Children; }
    public static readonly BindableProperty HeadingProperty =
            BindableProperty.Create(nameof(Heading), typeof(string),
            typeof(ContentFrame), null, propertyChanged: OnHeadingChanged);

    public string Heading
    {
        get => (Heading)GetValue(HeadingProperty);
        set => SetValue(HeadingProperty, value);
    }

static void OnHeadingChanged(BindableObject bindable, object oldValue, object newValue) {
//whatever you want to handle
 }

    public ContentFrame()
    {
        Content = contentStack;
        HasShadow = false;
        SetDynamicResource(BackgroundColorProperty, "ContentFrameBackgroundColor");
        SetDynamicResource(BorderColorProperty, "ContentFrameBorderColor");
        SetDynamicResource(CornerRadiusProperty, "ContentFrameCornerRadius");
    }
}

如果您不需要InotifypropertyChanged,您可以删除OnHeadingChanged(也可以从内部删除BindableProperty.Create(),如果需要,您可以添加以下声明。

于 2020-12-05T12:04:06.447 回答