Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

[All] Bindable span #1850

Merged

Conversation

adamped
Copy link
Contributor

@adamped adamped commented Feb 9, 2018

Description of Change

This allows Text to be Bindable on a Span.
It also added the Style property to the Span, to set the Font Elements.

fixes #1340

Bugs Fixed

N/A

API Changes

Added:

  • TextColor { get; set; } // Bindable Property

Marked Obsolete:

  • ForegroundColor { get; set; }
  • ForegroundColor now just updates TextColor

Behavioral Changes

N/A

PR Checklist

  • Has tests (if omitted, state reason in description)
  • Rebased on top of master at time of PR
  • Changes adhere to coding standard
  • Consolidate commits as makes sense

@samhouts samhouts changed the title Feature/1340 bindable span [All] Bindable span Feb 9, 2018
@samhouts samhouts added this to Ready in v3.1.0 via automation Feb 9, 2018
@samhouts samhouts moved this from Ready to In Review in v3.1.0 Feb 9, 2018
@@ -5,163 +5,160 @@

namespace Xamarin.Forms
{
public partial class VisualElement
sealed class MergedStyle : IStyle
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All I did here was remove the public partial class VisualElement line.

Didn't make any formatting changes, Github just thinks its all different.

{
var formattedString = (FormattedString)newvalue;
formattedString.PropertyChanged += ((Label)bindable).OnFormattedTextChanged;
SetInheritedBindingContext(formattedString, bindable.BindingContext);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes the BindingContext when it is updated on the Label.

{
base.OnBindingContextChanged();
for (int i = 0; i < Spans.Count; i++)
SetInheritedBindingContext(Spans[i], BindingContext);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sets BindingContext of Spans when the FormattedText BindingContext changes

formattedString.Spans.Add (new Span { BackgroundColor = Color.Red, ForegroundColor = Color.Olive, Text = "Span 1 " });
formattedString.Spans.Add (new Span { BackgroundColor = Color.Black, ForegroundColor = Color.White, Text = "Span 2 " });
formattedString.Spans.Add (new Span { BackgroundColor = Color.Pink, ForegroundColor = Color.Purple, Text = "Span 3" });
formattedString.Spans.Add (new Span { BackgroundColor = Color.Red, TextColor = Color.Olive, Text = "Span 1 " });
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are changed because the ForegroundColor is now obsolete, as per Stephane's request.

set { _fontElement.SetValue(FontElement.FontProperty, value); }
public static readonly BindableProperty TextColorProperty = TextElement.TextColorProperty;

public Color TextColor
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TextColor property now used, to align with rest of XF framework, from ITextElement interface.

Copy link

@jassmith jassmith left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks pretty good, there are plenty of places where double GetValue calls occur but those were there in the past too. If you want to go the extra mile and clean those up, great, otherwise its nowhere near a blocker.

@jassmith
Copy link

Im wondering if it might make sense to have all the bindings in here be OneTime by default. I doubt that generally speaking spans need dynamic binding and OneTime bindings are much cheaper. We have an opportunity here to make things fast by default in this area at least, even if we cant change it elsewhere.

Copy link
Member

@StephaneDelcroix StephaneDelcroix left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks good, but I think that, as the Parent/Children concept is missing, you won't be able to apply css styles, dynamic resources, ...

I can look at that when I have a few minutes, but in the mean time, do not merge this PR

@StephaneDelcroix StephaneDelcroix added the DO-NOT-MERGE-!!! 🛑 This is in progress and needs to be updated before it can be merged. label Feb 16, 2018
@jassmith
Copy link

Thats a really good point stephane. At the bare minimum the Parent will need to be set on the BindableSpan.

@adamped
Copy link
Contributor Author

adamped commented Feb 17, 2018

@StephaneDelcroix - I set the parent going down. The FormattedText was changed to inherit from Element, then parent set, and Parent set on all Spans.

@jassmith - I assume the GetValue multiple calls was the Text property is called a lot of times, I made minor changes to reduce the number of calls. Down to 1 on iOS and UWP. Down to 2 from 4 on Android.

As for OneTime bindings, I didn't know they existed. How would you implement them?

@jassmith
Copy link

Just set the default binding mode on the BindableProperty to be OneTime instead of not specifying it (which defaults to OneWay).

@adamped
Copy link
Contributor Author

adamped commented Feb 17, 2018

@jassmith - ok, added them. BindingMode.OneTime was only added 10 days ago, so I needed to rebase. Didn't know OneTime binding was available.

@jassmith
Copy link

Yeah its new :)

@@ -66,7 +66,7 @@ public sealed class BindableProperty
throw new ArgumentNullException("declaringType");

// don't use Enum.IsDefined as its redonkulously expensive for what it does
if (defaultBindingMode != BindingMode.Default && defaultBindingMode != BindingMode.OneWay && defaultBindingMode != BindingMode.OneWayToSource && defaultBindingMode != BindingMode.TwoWay)
if (defaultBindingMode != BindingMode.Default && defaultBindingMode != BindingMode.OneWay && defaultBindingMode != BindingMode.OneWayToSource && defaultBindingMode != BindingMode.TwoWay && defaultBindingMode != BindingMode.OneTime)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Woops

@smartprogrammer93
Copy link

Any Updates on this?
It's really needed

Thank you @adamped for your work

@jassmith
Copy link

jassmith commented Mar 6, 2018

build

}

[Obsolete("Foreground is obsolete as of version 2.6.0. Please use the TextColor property instead.")]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'll want to change this (and probably others in the repo) to 3.0.0 at some point).

@@ -56,9 +56,9 @@ private static string GenerateMarkupText(Span span)
}

// ForegroundColor =>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

obsolete comment

@rmarinho
Copy link
Member

rmarinho commented Mar 6, 2018

Build is failing related to docs issues

@adamped
Copy link
Contributor Author

adamped commented Mar 7, 2018

@rmarinho @jassmith - I updated the docs. Hopefully it works this time.

@rmarinho
Copy link
Member

rmarinho commented Mar 7, 2018

@adamped passed docs! thanks

@jassmith
Copy link

jassmith commented Mar 7, 2018

Good I want to get this in

@alanag13
Copy link
Contributor

alanag13 commented Mar 7, 2018

@jassmith me too, underlines are a big deal

@StephaneDelcroix
Copy link
Member

This is unlikely styleable using either Style or CSS as FromattedStrings and Spans aren't VisualElement.

@adamped: you don't have to act on this comment, I need to discuss how to fix this with @jassmith

@adamped
Copy link
Contributor Author

adamped commented Mar 8, 2018

@StephaneDelcroix - Styles work, I even have a unit test for it. Haven't tested on XAML with a Style though, but in code it works.

CSS I haven't tried. Haven't tested merged styles either.

@jassmith
Copy link

jassmith commented Mar 8, 2018

In she goes! Congratulations adam, this is a major feature!

@jassmith jassmith merged commit e42bd62 into xamarin:master Mar 8, 2018
v3.1.0 automation moved this from In Progress to Done Mar 8, 2018
Enhancements automation moved this from In Progress to Done Mar 8, 2018
@adamped
Copy link
Contributor Author

adamped commented Mar 8, 2018

Awesome, thanks @jassmith

Will forge ahead with tappable spans now, have slowly been working on them, will be ready for another review soon.

@jassmith
Copy link

jassmith commented Mar 9, 2018

excellent

@samhouts samhouts removed the DO-NOT-MERGE-!!! 🛑 This is in progress and needs to be updated before it can be merged. label Mar 23, 2018
@samhouts samhouts added the API-change Heads-up to reviewers that this PR may contain an API change label Apr 3, 2018
@floriankick
Copy link

I love this feature! When will this come to nuget?

@smartprogrammer93
Copy link

@findus

You can get it now on the nightly build.

From the xamarin nightly repository.

@samhouts samhouts added this to the 3.1.0 milestone May 5, 2018
@VincentH-Net
Copy link
Contributor

@adamped This is great. I'm really interested in tappable spans, do you still plan on implementing that? Eta? Thx!

@adamped
Copy link
Contributor Author

adamped commented May 19, 2018

@VincentH-Net - Its already implemented - #2173

@adamped adamped deleted the feature/1340-bindable-span branch May 19, 2018 09:10
@rzihe
Copy link

rzihe commented May 24, 2018

Really excited about this feature. Installed 3.1.0 pre release to test this out but I keep getting the following error: System.MissingFieldException: Field 'Xamarin.Forms.Span.TextProperty' not found.

My code is as followed:
<Label FontSize="Small" TextColor="Gray" > <Label.FormattedText> <FormattedString> <Span Text="{Binding User.Firstname}" FontAttributes="Bold" /> <Span Text="{Binding ResponseIsMatch}" /> <Span Text="{Binding Option.Title}" FontAttributes="Bold"/> </FormattedString> </Label.FormattedText> </Label>
what am I missing?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
API-change Heads-up to reviewers that this PR may contain an API change community-sprint F100 t/enhancement ➕
Projects
No open projects
v3.1.0
  
Done
Development

Successfully merging this pull request may close these issues.

[Enhancement] Bindable Span
10 participants