Skip to content

Instantly share code, notes, and snippets.

@mecvillarina
Forked from ads90/UIDirectionExtension
Created January 17, 2018 17:00
Show Gist options
  • Save mecvillarina/0412e17a7be0a62e2ee89ded8f265f34 to your computer and use it in GitHub Desktop.
Save mecvillarina/0412e17a7be0a62e2ee89ded8f265f34 to your computer and use it in GitHub Desktop.
RTL Support for Xamarin Forms
/*
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