CallygraphyXamarin无法在MvxAppCompatActivity中工作

cfl*_*cfl 2 xamarin.android mvvmcross xamarin

我已经尝试了几个小时来在我的MvvmCross项目中使用自定义字体,特别是Android平台.我成功安装了该组件并按照上述步骤操作:https: //components.xamarin.com/gettingstarted/calligraphyxamarin

它只是不想工作.

我在一项活动上测试了它,而不是从它继承Activity,AppCompatActivity它们都正常工作.好像从MvxAppCompatActivity休息中继承了吗?这个问题有什么解决办法?

Sve*_*übe 7

更新

我为此发布了一个nuget包(参见:MvvmCross.Calligraphy).

只需下载并修改您的设置,如:

public class Setup : MvxAndroidSetup
{
    protected override MvxAndroidBindingBuilder CreateBindingBuilder()
    {
        return new CalligraphyMvxAndroidBindingBuilder();
    }
}
Run Code Online (Sandbox Code Playgroud)

是的,因为MvvmCross使用自定义布局充气器来引入绑定和其他一些神奇的东西.这推出了书法充气机.不幸的是,我没有找到使用nuget package/xamarin组件的方法.您必须创建自己的绑定并使其CalligraphyFactory可用.

修改了metadata.xml

<attr path="/api/package[@name='uk.co.chrisjenx.calligraphy']/class[@name='CalligraphyFactory']" 
      name="visibility">public</attr>
Run Code Online (Sandbox Code Playgroud)

定制工厂

public class MyFactory : MvxAndroidViewFactory
{
    private CalligraphyFactory _factory;

    public MyFactory()
    {
        _factory = new Calligraphy.CalligraphyFactory(Resource.Attribute.fontPath);
    }

    public override View CreateView(View parent, string name, Context context, IAttributeSet attrs)
    {
        var view = base.CreateView(parent, name, context, attrs);
        view = _factory.OnViewCreated(view, context, attrs);
        return view;
    }
}
Run Code Online (Sandbox Code Playgroud)

自定义绑定构建器

class MyBindingBuilder : MvxAndroidBindingBuilder
{
    protected override IMvxAndroidViewFactory CreateAndroidViewFactory()
    {
        return new MyFactory();
    }
}
Run Code Online (Sandbox Code Playgroud)

Setup.cs

public class Setup : MvxAndroidSetup
{
    protected override MvxAndroidBindingBuilder CreateBindingBuilder()
    {
        return new MyBindingBuilder();
    }
    // ...
}
Run Code Online (Sandbox Code Playgroud)

活动

你不需要AttachBaseContext.不幸的是,似乎没有合作MvxAppCompatActivity,但有MvxActivity.我不确定导致这个问题的原因.

public class FirstView : MvxActivity
{
    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);

        CalligraphyConfig.InitDefault(new CalligraphyConfig.Builder()
           .SetDefaultFontPath("fonts/gtw.ttf")
           .SetFontAttrId(Resource.Attribute.fontPath)
           .DisablePrivateFactoryInjection()
           .Build());

        SetContentView(Resource.Layout.FirstView);
    }
}
Run Code Online (Sandbox Code Playgroud)

视图

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:local="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
  <EditText
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textSize="40dp"
    local:MvxBind="Text Hello"
    />
  <TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textSize="40dp"
    local:MvxBind="Text Hello"
    fontPath="fonts/gtw.ttf"
    />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

结果

自定义字体