Bindable Span

ChaseFlorellChaseFlorell CAInsider, University mod
edited May 27 in Xamarin.Forms Evolution

Summary

The Span class should have Bindable properties and the addition of a Style property.

API Changes

Make Span Inherit from BindableObject and support bindings on the properties. Also add a Style property so that Spans can be individually styled in the same way as a Label.

eg:

<Label>
    <Label.FormattedText>
        <FormattedString>
            <FormattedString.Spans>
                <Span Text="My Label" Style="{StaticResource title-style}" />
                <Span Text="{Binding Info}" Style="{StaticResource text-style}" />
                <Span Text="{Binding Sum}" Style="{StaticResource number-style}" />
            </FormattedString.Spans>
        </FormattedString>
    </Label.FormattedText>
</Label>

Intended Use Case

Bindable spans make for much simpler FormattedText. This is not the first time this has been requested.
https://xamarin.uservoice.com/forums/258559-xamarin-forms-suggestions/suggestions/6256703-extend-span-with-bindable-text-and-tapgesture
https://xamarin.uservoice.com/forums/258559-xamarin-forms-suggestions/suggestions/6622199-unseal-span-or-make-it-bindable

Also nice to have the ability to nicely format labels as follows.

SPANS-FTW

0
0 votes

Open · Last Updated

Posts

  • ChaseFlorellChaseFlorell CAInsider, University mod
    edited May 27

    As a work around, here's an example of how I have achieved the above.

    <!-- View -->
    <Label FormattedText="{Binding FormattedFoo}" />
    
    // viewmodel
    public string Foo
    {
        private get { return _foo; }
        set
        {
            if(SetField(ref _foo, value))
            {
                OnPropertyChanged(nameof(FormattedFoo));
            }
        }
    }
    
    public FormattedString FormattedFoo
    {
        get
        {
            return new FormattedStringBuilder()
                .Span("Foo Label", "My-Title-Style")
                .Span(Foo, "My-Normal-Style")
                .Build();
        }
    }
    

    FormattedStringBuilder();

  • StephaneDelcroixStephaneDelcroix USInsider, Beta ✭✭✭✭

    updating a Span probably means redrawing the full Label on most platform, so this has an hidden performance implication, but I kinda like this proposal.

    could you please develop a bit more on how you plan to support Gestures on Span on the main platforms ?

  • mdizzymdizzy USMember ✭✭

    I have a more generic proposal that might help here. I've been working on graphics primitives as XAML elements. I think Views are expensive but visualelement isn't exposed on the API surface. I've got an idea for how it all works though (proposal coming soon).

  • ChaseFlorellChaseFlorell CAInsider, University mod

    @StephaneDelcroix said:
    could you please develop a bit more on how you plan to support Gestures on Span on the main platforms ?

    I have edited this proposal to remove Gestures. Once I have a better idea on how it would work on the platforms, I will submit as a separate proposal

  • ChaseFlorellChaseFlorell CAInsider, University mod

    @StephaneDelcroix since I've removed the gesture portion of this proposal. What are your thoughts on just introducing the bindable Span?

  • VelocityVelocity NZMember ✭✭✭

    +1. Very useful.

  • David.RettenbacherDavid.Rettenbacher ATMember ✭✭

    +1

  • TobiasSchulz.9796TobiasSchulz.9796 DEMember ✭✭

    Very useful!

  • PradeepSJPradeepSJ USMember ✭✭

    +1 useful

  • AndrewMobileAndrewMobile USMember ✭✭✭✭

    +1000

  • BobbyTablesBobbyTables GBMember ✭✭

    +1001

  • NamyslawSzymaniukNamyslawSzymaniuk USMember ✭✭✭

    Would be really useful. +1002

Sign In or Register to comment.