我可以合并Xaml<;标签>;到我用来创建框架的C#模板中?

我可以合并Xaml<;标签>;到我用来创建框架的C#模板中?,c#,xaml,xamarin,xamarin.forms,bindableproperty,C#,Xaml,Xamarin,Xamarin.forms,Bindableproperty,我有XAML代码,用于设置帧和帧标题,如下所示: <Label Text="ABC" /> <t:ContentFrame> <Label Text="ABC" /> </t:ContentFrame> 是否有一种方法可以将标题的设置组合到ContentFrame类中,从而实现相同的功能: <t:ContentFrame Heading="ABC"> <La

我有XAML代码,用于设置帧和帧标题,如下所示:

<Label Text="ABC" />
<t:ContentFrame>
   <Label Text="ABC" />
</t:ContentFrame>
是否有一种方法可以将标题的设置组合到
ContentFrame
类中,从而实现相同的功能:

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

您需要一个

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");
    }
}
公共类ContentFrame:CustomFrame
{
StackLayout contentStack{get;}=new StackLayout()
{
间距=0,
填充=新厚度(0),
方向=堆叠方向。垂直
};
公共IList内容{get=>contentStack.Children;}
公共静态只读BindableProperty HeadingProperty=
创建(名称(标题)、类型(字符串),
typeof(ContentFrame),null,propertyChanged:OnHeadingChanged);
公共字符串标题
{
get=>(标题)GetValue(标题属性);
set=>SetValue(HeadingProperty,value);
}
静态无效OnHeadingChanged(BindableObject bindable、object oldValue、object newValue){
//不管你想处理什么
}
公共内容框架()
{
Content=contentStack;
Hassadow=假;
SetDynamicSource(BackgroundColorProperty,“ContentFrameBackgroundColor”);
SetDynamicSource(BorderColorProperty,“ContentFrameBorderColor”);
SetDynamicSource(CornerRadiusProperty,“ContentFrameCornerRadius”);
}
}

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

您可以在问题的C代码中给出一个示例,这样我就可以看到如何进行此操作的完整图片。ThanksI是指公共类ContentFrame的完整代码。我认为清楚地说明这一点很好,这样其他人就可以看到如何添加所需内容。我认为这是probab我投了反对票,因为你只是给出了一段代码并使用了…而不是添加实际需要的内容。如果你想添加完整的代码,我很乐意接受答案。谢谢。很抱歉,你最初的问题没有澄清这一点,我会尝试。我添加了这个新问题
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");
    }
}