Created
May 31, 2020 05:31
-
-
Save haavamoa/4cc5d40a3630609e2e59737a6e56202b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- Using static strings (like localized string) --> | |
<Label Text="{namespace:StringCase Input={x:static LocalizedStrings.MyString} StringCase=Upper}" /> | |
<!-- Using binding --> | |
<Label Text={"Binding MyString, Converter={namespace:StringCaseConverter StringCase=Upper}" /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace Visit.Mobile.MarkupExtensions | |
{ | |
/// <summary> | |
/// A string case extension that can be used in XAML with static values (like localized strings). | |
/// Using this is the same as using a string case converter for bindings. | |
/// </summary> | |
[ContentProperty(nameof(Input))] | |
public class StringCaseExtension : IMarkupExtension | |
{ | |
public string Input { get; set; } | |
public StringCase StringCase { get; set; } | |
public object ProvideValue(IServiceProvider serviceProvider) | |
{ | |
if (string.IsNullOrEmpty(Input)) return string.Empty; | |
switch (StringCase) | |
{ | |
case StringCase.None: | |
return Input; | |
case StringCase.Upper: | |
return CultureInfo.CurrentCulture.TextInfo.ToUpper(Input); | |
case StringCase.Lower: | |
return CultureInfo.CurrentCulture.TextInfo.ToLower(Input); | |
case StringCase.Title: | |
return CultureInfo.CurrentCulture.TextInfo.ToTitleCase(Input); | |
default: | |
throw new ArgumentOutOfRangeException(); | |
} | |
} | |
} | |
public enum StringCase | |
{ | |
None = 0, | |
Upper, | |
Lower, | |
Title, | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace Visit.Mobile.Converters | |
{ | |
public class StringCaseConverter : IMarkupExtension, IValueConverter | |
{ | |
public StringCase StringCase { get; set; } | |
public object ProvideValue(IServiceProvider serviceProvider) => this; | |
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) | |
{ | |
if (!(value is string stringValue)) throw new Exception("Input has to be of type string"); | |
if (stringValue == string.Empty) return string.Empty; | |
var stringCaseExtension = new StringCaseExtension() { Input = stringValue, StringCase = StringCase}; | |
#nullable disable | |
return stringCaseExtension.ProvideValue(null); | |
#nullable restore | |
} | |
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) | |
{ | |
throw new NotImplementedException(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment