Xamarin 如何创建一个模板/类来简化Xaml中的一些元素,其中多个元素只包含一个元素?
下面是我现在需要做的一个例子。有时我有一个跨度,有时更多 请注意,这篇文章与另一个问题类似。对于另一个问题,我只有一条注释可以使用自定义控件,没有更多的建议,只有一条答案可以使用JavaScript。我试图在这个问题上再加上一个悬赏,但它只给了我500分的悬赏。这个问题已经太老了,我怀疑任何人都不会再看到它了,因为我不能增加悬赏(除非是500分),我不能给它更多的可见性 以下是我想简化的内容:Xamarin 如何创建一个模板/类来简化Xaml中的一些元素,其中多个元素只包含一个元素?,xamarin,xamarin.forms,Xamarin,Xamarin.forms,下面是我现在需要做的一个例子。有时我有一个跨度,有时更多 请注意,这篇文章与另一个问题类似。对于另一个问题,我只有一条注释可以使用自定义控件,没有更多的建议,只有一条答案可以使用JavaScript。我试图在这个问题上再加上一个悬赏,但它只给了我500分的悬赏。这个问题已经太老了,我怀疑任何人都不会再看到它了,因为我不能增加悬赏(除非是500分),我不能给它更多的可见性 以下是我想简化的内容: <Label> <Label.FormattedText>
<Label>
<Label.FormattedText>
<FormattedString>
<Span Text="Hello " />
<Span Text="Hello " />
<Span Text=" Some more text." />
</FormattedString>
</Label.FormattedText>
</Label>
我如何才能完成我所寻找的目标?您可以执行以下操作:
//FormattedLabel.xaml.cs
[ContentProperty(名称(跨度))]
公共分部类FormattedLabel:标签
{
私有只读可观察集合_span;
公共IList跨度=>\u跨度;
公共格式化标签()
{
_span=新的ObservableCollection();
_span.CollectionChanged+=onspan已更改;
初始化组件();
}
已更改的私有void(对象发送方,NotifyCollectionChangedEventArgs e)
{
格式化文本?.span?.Clear();
FormattedText=FormattedText??新的FormattedString();
ForEach(FormattedText.Spans.Add);
}
}
基本上,在Label
的这个扩展中,我们将内容定义为Span
项的列表,这将允许您在
内部的XAML中定义它们。为了使其工作,我们将这些项传递给this.FormattedText.span
要使用它:
我刚刚检查过,它工作得很好。我希望这有帮助 你的截图链接不起作用嗨,我没有截图链接。像这样的词只是指下面的文本。我会改成用不同的词。谢谢你指出。这有用吗@Alan2That适用于实现ContentView的某些东西,但我认为它在这种情况下没有帮助。您刚才不是问了一个与此完全相同的问题吗?然后我指示你,为什么那不做你想做的?
<template:FormattedLabel>
<Span Text="Hello " />
<Span Text="Hello " />
<Span Text=" Some more text." />
</template:FormattedLabel>
<template:FormattedLabel>
<Span Text="Hello " />
</template:FormattedLabel>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:Japanese;assembly=J"
xmlns:t="clr-namespace:J.Templates"
x:Class="Japanese.Templates.ContentScrollable"
x:Name="ContentPage" >
<ContentPage.Content>
<t:Stack Orientation="Vertical">
<ScrollView x:Name="scroll">
<ContentView Content="{Binding Source={x:Reference ContentPage}, Path=InnerContent}"
Margin="{DynamicResource PageMargin}" />
</ScrollView>
</t:Stack>
</ContentPage.Content>
</ContentPage>
public partial class ContentScrollable : ContentPage
{
public static readonly BindableProperty InnerContentProperty = BindableProperty.Create(nameof(InnerContent), typeof(View), typeof(ContentScrollable));
public View InnerContent
{
get => (View)this.GetValue(InnerContentProperty);
set => this.SetValue(InnerContentProperty, value);
}
public ContentScrollable()
{
InitializeComponent();
}
}