-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Adds FontImage Markup Extension for FontImageSource #6398
Conversation
@@ -24,6 +24,8 @@ public object Parse(string match, ref string remaining, IServiceProvider service | |||
markupExtension = new OnIdiomExtension(); | |||
else if (match == "DataTemplate") | |||
markupExtension = new DataTemplateExtension(); | |||
else if (match == "FontImage") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not required. I'm not even sure we save any measurable time doing this
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can remove this, no problem
|
||
namespace Xamarin.Forms.Xaml | ||
{ | ||
[AcceptEmptyServiceProvider] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
❤️
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd add the Glyph
as ContentProperty
attribute
[AcceptEmptyServiceProvider] | |
[AcceptEmptyServiceProvider] | |
[ContentProperty("Glyph")] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not my first MarkupExtension and I noticed the other platform ones have [ContentProperty]
but I am not sure exactly what it is for. How is it used?
Adding this change is not a problem at all
@StephaneDelcroix I have the Xamarin.Forms.Sandbox project working with this change but didn't commit it. Should I commit that change as well to make it easier for you to see this change in action? |
ContentProperty makes it so that you don't have to define |
@samhouts @StephaneDelcroix I am thinking about adding the property ( |
I think all options need to be there, or the extension isn't fully useful. (side note: default 30 for size is absurd). I also put together a proposal whereby we could set some defaults in order to keep verbosity down. |
I left it out originally because my target use case was for the Shell |
@ahoefling having all properties exposed adds versatility. that's what we want (see Binding). If all the options were mandatory, that would be verbose, but that's not the case here |
I gave this a try and got a strange result. <!-- works good -->
<ImageButton Source="{FontImage FontFamily={StaticResource MyFontFamily}, Glyph={x:Static xml:Images.SmileFace}, Color={StaticResource White}" /> but <!-- doesn't properly set Glyph -->
<ImageButton Source="{FontImage {x:Static xml:Images.SmileFace}, FontFamily={StaticResource MyFontFamily}, Color={StaticResource White}" /> In other words, the |
Could it be because we are using [ContentProperty(nameof(Glyph))] instead of [ContentProperty("Glyph")] I don't think this would cause that, but I don't know much about how the |
I just added more unit tests to cover this and did some manually testing as well. It appears that
Yes, that is correct and it is missing the trailing @ChaseFlorell I think you discovered a bug with how the |
I'm going to be submitting a proposal today that will require that the Once the proposal is in, I will link back to this for further discussion. |
After running several tests today I don't see a lot of value in I'm recommending that we remove the |
public string FontFamily { get; set; } | ||
public string Glyph { get; set; } | ||
public Color Color { get; set; } | ||
public double Size { get; set; } = 30d; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not sure about setting a default. does FontImageSource requires a Size ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Size already defaults to 30 on FontImageSource, but if you don't set the default here, it'll revert to 0
.
Perhaps you can use FontImageSource.SizeProperty.DefaultValue
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
alternatively, I don't know if FontImageSource has a concept of a magic value -1
to roll over to default.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My thought process behind this is the default size for FontImageSource
is 30d
. If someone uses this markup extension it will default to 0 unless they explicitly provide a size.
Perhaps we add a static default in FontImageSource
and that way both the markup extension and FontImageSource
can reference the default size.
FontImageSource
public class FontImageSource : ImageSource
{
// ...
public static double DefaultSize = 30d;
// ...
}
FontImageExtension
public class FontImageExtension : IMarkupExtension<ImageSource>
{
// ...
public double Size { get; set; } = FontImageSource.DefaultSize;
// ...
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can access it directly off of the SizeProperty
public double Size { get; set; } = (double)FontImageSource.SizeProperty.DefaultValue;
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is a great suggestion, I'll go an update that to use the FontImageSource.SizeProperty.DefaultValue
…alue. This will keep it in sync if that value ever changes
The ContentProperty has to be a literal, not another markup extension. I don't think it's bug |
Is there a reason why ContentProperty doesn't support MarkupExtensions? Is this something that should be included in a proposal and feature request? |
Here are the reasons I can think of:
Now, if I suggested adding a ContentProperty not to support StaticResource for the Glyph, but for the cases when you prefer to use the glyph code instead of a resource. |
@StephaneDelcroix thanks for the detailed explanation, I am not as familiar with the reasoning behind it and wanted to understand better. At this point I am in agreement with adding <ImageButton Source="{FontImage Glyph={x:Static xml:Images.SmileFace}}" /> The ContentProperty is added here as a convenience utility for people that want to use a string literal |
Question... Source="{FontImage FontFamily={DynamicResource MaterialFontFamily},
Glyph={StaticResource IconUpNext},
Color={StaticResource PrimaryColor},
Size=15}" <OnPlatform x:Key="MaterialFontFamily" x:TypeArguments="x:String">
<On Platform="iOS" Value="Material Design Icons" />
<On Platform="Android" Value="materialdesignicons-webfont.ttf#Material Design Icons" />
<On Platform="UWP" Value="Assets/Fonts/materialdesignicons-webfont.ttf#Material Design Icons" />
</OnPlatform> Compiler gives error about FontFamily: if I changeto StaticResource compiles, but throws an exception at runtime. |
the extension is fully resolved at the time it's parsed, that's why DynamicResource won't work |
Gotcha... any way to solve this to clean it up? Suggestions? |
Something like: FontFamily={OnPlatform
iOS={StaticResource MaterialFontFamilyiOS},
Android={StaticResource MaterialFontFamilyAndroid}} ? |
@jamesmontemagno don't use the markup extension, it doesn't provide a lot of benefits as it doesn't save a lot of precious keystrokes |
Yeah, but it looks better! Can i define a StaticResource and update it in the App.xaml.cs? |
NM it all works with StaticResource in place :) yay... for some reason Xamarin.Forms didn't update when I updated Xamarin.Forms.Visual.Material... weird. |
Description of Change
Adds Markup Extension for
FontImageSource
to simplify usage from verbose XAMLIssues Resolved
API Changes
Added MarkupExtension:
FontImageExtension
toXamarin.Forms.Xaml
Added:
Platforms Affected
FontImageSource
Behavioral/Visual Changes
None
Before/After Screenshots
Not applicable
Testing Procedure
This markup extension has been tested in the Sandbox project and a confidential project I am working on.
I added positive and negative unit tests in the
Xamarin.Forms.Xaml.UnitTests
projectPR Checklist