Xaml - 如何与类绑定&渲染器

时间:2016-11-02 17:24:55

标签: c# wpf xaml xamarin

我在修改下面的代码时遇到了一个小问题。它工作正常,但我必须在nIcon类中永久设置字体。我想将它用于我的xaml文件中的不同字体。

在下面的示例代码中,我希望字体从默认的“FontAwesome”更改为“Ionicons”,如xaml文件中所述。

由于Xaml在创建类时不接受参数,因此我不知道如何设置它。

有什么想法吗?

Xaml代码:

<controls:nIcon 
    Text="\nf011"
    FontFamily="Ionicons"/>

共享代码:

public class nIcon : Label
{
    public const string Typeface = "FontAwesome";

    public nIcon()
    {
       \\ FontFamily = Typeface;
    }
}

Android渲染器:

public class IcDroidRenderer : LabelRenderer
{
    protected override void OnElementChanged(ElementChangedEventArgs<Label> e)
    {
        base.OnElementChanged(e);
        if (e.OldElement == null)
        {
            Control.Typeface = Typeface.CreateFromAsset(Forms.Context.Assets, nIcon.Typeface + ".ttf");
        }
    }
}

0 个答案:

没有答案