Last active
March 18, 2018 11:36
-
-
Save ads90/ff5c69410d2654dcad3ed8d275d7ca20 to your computer and use it in GitHub Desktop.
RTL Support for Xamarin Forms
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
/* | |
how to use it | |
you have to design your xaml to fit your need | |
EX: | |
<ColumnDefinition Width="{UIExtensions:UIDirection Type=GridLength,LTR=auto ,RTL=*}" /> | |
HorizontalTextAlignment="{UIExtensions:UIDirection Type=TextAlignment,LTR=Start,RTL=End}" | |
HorizontalOptions="{UIExtensions:UIDirection Type=LayoutOption, LTR=Start, RTL=End}" | |
*/ | |
using System; | |
public class UIDirectionExtension : IMarkupExtension | |
{ | |
private static GridLengthTypeConverter _gridLengthTypeConverter = new GridLengthTypeConverter(); | |
private static LayoutOptionsConverter _layoutOptionsConverter = new LayoutOptionsConverter(); | |
private static ThicknessTypeConverter _thicknessTypeConverter = new ThicknessTypeConverter(); | |
public DataType Type { get; set; } | |
public object RTL { get; set; } | |
public object LTR { get; set; } | |
public object ProvideValue(IServiceProvider serviceProvider) | |
{ | |
switch (Type) | |
{ | |
case DataType.GridLength: | |
{ | |
if (Surface.IsRTL) | |
{ | |
return _gridLengthTypeConverter.ConvertFromInvariantString(RTL.ToString()); | |
} | |
else | |
{ | |
return _gridLengthTypeConverter.ConvertFromInvariantString(LTR.ToString()); | |
} | |
} | |
case DataType.LayoutOption: | |
{ | |
if (Surface.IsRTL) | |
{ | |
return _layoutOptionsConverter.ConvertFromInvariantString(RTL.ToString()); | |
} | |
else | |
{ | |
return _layoutOptionsConverter.ConvertFromInvariantString(LTR.ToString()); | |
} | |
} | |
case DataType.TextAlignment: | |
{ | |
if (Surface.IsRTL) | |
{ | |
return TryParseTextAlignement(RTL.ToString()); | |
} | |
else | |
{ | |
return TryParseTextAlignement(LTR.ToString()); | |
} | |
} | |
case DataType.Int: | |
{ | |
if (Surface.IsRTL) | |
{ | |
return Convert.ToInt32(RTL); | |
} | |
else | |
{ | |
return Convert.ToInt32(LTR); | |
} | |
} | |
case DataType.ResourceKey: | |
if (Surface.IsRTL) | |
{ | |
return LocalizeExtension.TranslateOrDefault(RTL.ToString()); | |
} | |
else | |
{ | |
return LocalizeExtension.TranslateOrDefault(LTR.ToString()); | |
} | |
case DataType.String: | |
{ | |
if (Surface.IsRTL) | |
{ | |
return RTL.ToString(); | |
} | |
else | |
{ | |
return LTR.ToString(); | |
} | |
} | |
case DataType.Thickness: | |
{ | |
if (Surface.IsRTL) | |
{ | |
return _thicknessTypeConverter.ConvertFromInvariantString(RTL.ToString()); | |
} | |
else | |
{ | |
return _thicknessTypeConverter.ConvertFromInvariantString(LTR.ToString()); | |
} | |
} | |
default: | |
break; | |
} | |
return ""; | |
} | |
private TextAlignment TryParseTextAlignement(string val) | |
{ | |
bool parsingResult = Enum.TryParse<TextAlignment>(val, out TextAlignment alignemet); | |
if (!parsingResult) | |
{ | |
throw new ZindRuntimeException($"{val} couldn't be recognized as TextAlignment."); | |
} | |
return alignemet; | |
} | |
} | |
public enum DataType | |
{ | |
GridLength, | |
LayoutOption, | |
TextAlignment, | |
Int, | |
String, | |
ResourceKey, | |
Thickness | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
are you try it & worked well ?
i tried but no action is happen