c# - 我可以将 Xaml 组合
问题描述
我有 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>
解决方案
为此,您需要一个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()
,如果需要,您可以添加以下声明。
推荐阅读
- java - 交换数组Java中的元素
- reactjs - React-Multiple 输入
- mechanicalturk - mturk 中可能有多个人群分类器元素吗?
- redis - 在 Python/Django 上使用 Redis 的 Celery 不填充任何 Redis 列表
- python - 线性回归 - 张量流
- python - Python函数:我试图取两个数字并返回True,如果其中一个参数是偶数并且它们都小于9
- php - 使用准备好的语句时是否需要过滤输入/htmlentities?
- html - 如何更改链接时您的网站所说的内容?
- sql - 每个基金和客户需要的平均值
- amazon-web-services - 在 terragrunt 配置中使用变量