xamarin - CallygraphyXamarin 无法在 MvxAppCompatActivity 中工作

标签 xamarin xamarin.android mvvmcross

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

它只是不想工作。

我在继承自 ActivityAppCompatActivity 的事件上对其进行了测试,它们都可以正常工作。 似乎从 MvxAppCompatActivity 继承会破坏它?这个问题有什么解决办法吗?

最佳答案

更新

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

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

public class Setup : MvxAndroidSetup
{
    protected override MvxAndroidBindingBuilder CreateBindingBuilder()
    {
        return new CalligraphyMvxAndroidBindingBuilder();
    }
}
<小时/>

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

修改后的metadata.xml

<attr path="/api/package[@name='uk.co.chrisjenx.calligraphy']/class[@name='CalligraphyFactory']" 
      name="visibility">public</attr>

定制工厂

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;
    }
}

自定义绑定(bind)构建器

class MyBindingBuilder : MvxAndroidBindingBuilder
{
    protected override IMvxAndroidViewFactory CreateAndroidViewFactory()
    {
        return new MyFactory();
    }
}

Setup.cs

public class Setup : MvxAndroidSetup
{
    protected override MvxAndroidBindingBuilder CreateBindingBuilder()
    {
        return new MyBindingBuilder();
    }
    // ...
}

事件

您不需要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);
    }
}

查看

<?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>

结果

custom font

关于xamarin - CallygraphyXamarin 无法在 MvxAppCompatActivity 中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42105583/

相关文章:

c# - Android启动后台服务时如何传值?使用 Xamarin 和 C#

android - Xamarin:无法在中构建干净的 Android Wear 项目

c# - Xamarin.Forms - 完全在 XAML 中的 ListView

c# - Monodroid WebView 加载 SSL 页面

c# - 多线程应用调试问题

ios - 窗口没有填满整个屏幕

xamarin - Visual Studio for Mac 中的 SiriKit 为 "No matching profiles found"

android - 单体机器人 : Global Error Handler

c# - MVVMCross如何实现ContentDialog

ios - 在设备上调试时应用程序崩溃