【问题标题】:How can I create a template / class that will allow me to simplify some my Xaml where there are multiple elements into just one?如何创建一个模板/类,让我可以将一些 Xaml 中的多个元素简化为一个?
【发布时间】:2019-05-07 01:03:03
【问题描述】:

这是我现在需要做的一个例子。有时我有一个跨度,有时更多。

请注意,此帖子与另一个问题的帖子相似。对于另一个问题,我只有一条评论来使用自定义控件,没有提供更多建议,并且有一个使用 JavaScript 的答案。我试图为该问题添加第二个赏金,但它让我可以选择仅添加 500 分的赏金。这个问题现在太老了,我怀疑有人会再看到它,而且因为我不能添加赏金(除非它是 500 分),所以我不能给它更多的可见性。

以下是我想简化的内容:

<Label>
   <Label.FormattedText>
      <FormattedString>
         <Span Text="Hello " />
         <Span Text="Hello " />
         <Span Text=" Some more text." />
      </FormattedString>
   </Label.FormattedText>
</Label>

这是我想要做的,而不是输入&lt;Label&gt;&lt;Label.FormattedText&gt;&lt;FormattedString&gt; 我想通过只输入&lt;template:FormattedLabel&gt; 获得一些方法来做到这一点

<template:FormattedLabel>
   <Span Text="Hello " />
   <Span Text="Hello " />
   <Span Text=" Some more text." />
</template:FormattedLabel>

<template:FormattedLabel>
   <Span Text="Hello " />
</template:FormattedLabel>

请注意,我已经研究了自定义控件,但据我所知,我无法找到一种方法让这些控件接受一些内部内容,在这种情况下,这些内容将是一个或多个跨度。

我有一个类似的例子,也许可以使用,但我不知道如何应用它。我希望在下面的 XAML 中有一个这样的模板,它的功能与我需要的类似,但用于内容页面:

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

使用其 C# 后端:

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

我怎样才能完成我正在寻找的东西?

【问题讨论】:

标签: xamarin xamarin.forms


【解决方案1】:

您可以执行以下操作:

<!-- FormattedLabel.xaml -->
<?xml version="1.0" encoding="UTF-8"?>
<Label 
    xmlns="http://xamarin.com/schemas/2014/forms" 
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    x:Class="YourNamespaceForTemplates.FormattedLabel" />
// FormattedLabel.xaml.cs
[ContentProperty(nameof(Spans))]
public partial class FormattedLabel : Label
{
    private readonly ObservableCollection<Span> _spans;
    public IList<Span> Spans => _spans;

    public FormattedLabel()
    {
        _spans = new ObservableCollection<Span>();
        _spans.CollectionChanged += OnSpansChanged;

        InitializeComponent();
    }

    private void OnSpansChanged(object sender, NotifyCollectionChangedEventArgs e)
    {
        FormattedText?.Spans?.Clear();
        FormattedText = FormattedText ?? new FormattedString();
        Spans.ForEach(FormattedText.Spans.Add);
    }
}

基本上,在Label 的这个扩展中,我们将内容定义为Span 项目的列表,这将允许您在&lt;FormattedLabel&gt;&lt;/FormattedLabel&gt; 内的XAML 中定义它们。为了让它发挥作用,我们将这些项目传递给this.FormattedText.Spans

使用它:

<template:FormattedLabel>
    <Span Text="Hello " />
    <Span Text="Hello " />
    <Span Text=" Some more text." />
</template:FormattedLabel>

我刚刚检查过它,它运行良好。我希望这会有所帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-28
    • 2018-04-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多