2015-04-07 36 views
0

我試圖實現從DatePicker派生的自定義控件。此控件將具有Android和iOS渲染器。我需要爲這個控件添加FontSize屬性,我將在渲染器類中使用它。自定義FontSize應接受字符串文字

這是我實現字號屬性的:

public static BindableProperty FontSizeProperty = BindableProperty.Create<ExDatePicker, double>(o => o.FontSize, 16d, propertyChanged: OnFontSizeChanged); 

public double FontSize 
    { 
     get { return (double)GetValue(FontSizeProperty); } 
     set { SetValue(FontSizeProperty, value); } 
    } 

private static void OnFontSizeChanged(BindableObject bindable, double oldvalue, double newvalue) 
    { 
     var control = bindable as ExDatePicker; 
     if (control != null) 
     { 
      control.FontSize = newvalue; 
     } 
    } 

我需要的是同樣的方式來使用這個屬性能力作爲本地字號性質的作品,即我需要可以設置類似

FontSize="Small" 

in xaml code。 這可能嗎?

編輯: 在xamarin表單中,可以使用FontSize =「Small」來設置不同平臺的設備特定字體大小。這autmagically轉換「小」串入雙用

Device.GetNamedSize(NamedSize.Small, typeof(ExtednedDatePicker)) 

我不知道如何添加這個自動轉換爲創建字號不Xamarin用戶窗體庫

+1

在這種情況下,你能不能有字體大小屬性作爲一個對象,並在你的'OnFontSizeChanged'方法嘗試確定它是否是'string'或'double',如果它是一個字符串使用'開關聲明「來設置大小? – user1

+0

將檢查此解決方案 – Dzior

+0

嘗試像這樣在xaml頁面FontSize =「{StaticResource FontSizeSmall}」添加此資源 16 – Jayasri

回答

-3

我以前從來沒有嘗試過xamarin。但我認爲如果你使用具有泛型類型參數的類型轉換器是可能的。

爲exmple,

public class StringToDoubleTypeConverter<T> : TypeConverter where T : IConvertible 
{ 
    // Other methods 
    ... 

    // Returns whether the type converter can convert an object from the specified type 
    // to the type of this converter. 
    public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) 
    { 
     return sourceType.GetInterface("IConvertible", false) != null; 
    } 

    // Returns whether the type converter can convert an object to the specified type. 
    public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) 
    { 
     return destinationType.GetInterface("IConvertible", false) != null; 
    } 

    public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) 
    { 
     try 
     { 
      var convertible = (IConvertible)value; 
      if (convertible != null) return convertible.ToType(typeof(T), culture); 
     } 
     catch (FormatException) 
     { 
      if (value != null && value.ToString().Equals("Small")) 
      { 
       return MyConstants.Small; 
      } 
      throw; 
     } 
     return null; 
    } 

    // Converts the specified value object to the specified type. 
    public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) 
    { 
     return ((IConvertible)value).ToType(destinationType, culture); 
    } 
} 

然後在你的字號屬性使用它。

[TypeConverter(typeof(StringToDoubleTypeConverter<double>))] 
public double FontSize 
{ 
    get { return (double)GetValue(FontSizeProperty); } 
    set { SetValue(FontSizeProperty, value); } 
} 

參考: http://www.kunal-chowdhury.com/2013/02/autotodouble-typeconverter-for-xaml-silverlight-wpdev.html

+0

鏈接被打破 – user1

+0

我更新了鏈接和答案 – Apple

0

試試這個轉換器

public object Convert(object value, Type targetType, object parameter, string language) 
     { 
      return (bool)value ? "22" : "25"; 
     } 

呼叫thsi轉換器

0

XAML頁面這是怎麼及其與Xamarin形式NamedSize完成。

NamedSizeEnum而不是string。儘管在XAML中使用了字符串轉換。

public enum NamedSize 
{ 
    Default = 0, 
    Micro = 1, 
    Small = 2, 
    Medium = 3, 
    Large = 4 
} 

它使用Device.GetNamedSize()從輸入值中獲取double值。 Device.GetNamedSize使用平臺服務或本地代碼進行轉換。

public class FontSizeConverter : TypeConverter, IExtendedTypeConverter 
{ 
    object IExtendedTypeConverter.ConvertFromInvariantString(string value, IServiceProvider serviceProvider) 
    { 
     if (value != null) 
     { 
      double size; 
      if (double.TryParse(value, NumberStyles.Number, CultureInfo.InvariantCulture, out size)) 
       return size; 
      NamedSize namedSize; 
      if (Enum.TryParse(value, out namedSize)) 
      { 
       Type type; 
       var valueTargetProvider = serviceProvider.GetService(typeof(IProvideValueTarget)) as IProvideValueTarget; 
       type = valueTargetProvider != null ? valueTargetProvider.TargetObject.GetType() : typeof(Label); 
       return Device.GetNamedSize(namedSize, type, false); 
      } 
     } 
     throw new InvalidOperationException(string.Format("Cannot convert \"{0}\" into {1}", value, typeof(double))); 
    } 

    public override object ConvertFromInvariantString(string value) 
    { 
     if (value != null) 
     { 
      double size; 
      if (double.TryParse(value, NumberStyles.Number, CultureInfo.InvariantCulture, out size)) 
       return size; 
      NamedSize namedSize; 
      if (Enum.TryParse(value, out namedSize)) 
       return Device.GetNamedSize(namedSize, typeof(Label), false); 
     } 
     throw new InvalidOperationException(string.Format("Cannot convert \"{0}\" into {1}", value, typeof(double))); 
    } 
}