我已经尝试了几个小时,让自定义字体在我的 MvvmCross 项目中工作,特别是 Android 平台。 我成功安装了该组件并按照上述步骤操作: https://components.xamarin.com/gettingstarted/calligraphyxamarin
它只是不想工作。
我在继承自 Activity
和 AppCompatActivity
的事件上对其进行了测试,它们都可以正常工作。
似乎从 MvxAppCompatActivity
继承会破坏它?这个问题有什么解决办法吗?
最佳答案
更新
我为此发布了一个 nuget 包(请参阅:MvvmCross.Calligraphy)。
- 示例项目:https://github.com/smstuebe/mvvmcross-calligraphy .
- 博客文章:http://smstuebe.de/2017/02/12/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>
结果
关于xamarin - CallygraphyXamarin 无法在 MvxAppCompatActivity 中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42105583/