本文介绍了Xamarin:确定GetSpans()中的元素是粗体还是斜体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

原生Android Spanned.getSpans(......,SyleSpan.class)函数返回类型StyleSpan[]

Xamarin ISpanned.GetSpans(......)函数返回类型Java.lang.Object[],尽管它在本机android中返回<T>(在我的情况下为T = StyleSpan).因此,由于Mono接口无法显示我使用本机SDK时所显示的内容,因此会丢失信息.

Xamarin ISpanned.GetSpans(......) function returns type Java.lang.Object[] though it returns <T> (T=StyleSpan in my case) in native android. Therefore there is a loss of information since the Mono interface doesn't expose what it would have been exposed if I had used the native SDK.

由于属性Style(在本机android中为getStyle())仅在StyleSpan中可用,因此无法读取通过GetSpans读取的给定StyleSpan是粗体还是斜体.

Since propery Style (getStyle() in native android) is only available in StyleSpan there is no way to read that a given StyleSpan read through GetSpans is bold or italic.

关于如何确定粗体斜体的任何想法?

Any ideas how I determine bold or italic?

这是单声道界面的限制吗?

Is this a limitation in the mono interface?

推荐答案

您可以做所有事情. ;)GetSpans方法没有舒适的通用包装.

You can do everything. ;) There is just no comfortable generic wrapper for the GetSpans method.

ISpanned ss = ...;
var spans = ss.GetSpans(0, 20, Class.FromType(typeof(SyleSpan)));
foreach (SyleSpan span in spans)
{
    // do what you want
    if(span.Style == TypefaceStyle.Bold)
    {
        Debug.WriteLine("Xamarin can find bold spans, too :)");
    }
}

如果您想以常规方式访问它:

if you want to access it generic:

public static class ISpannedExtension
{
    public static TSpan[] GetSpans<TSpan>(this ISpanned ss, int startIndex, int length)
    {
        return ss.GetSpans(startIndex, length, Class.FromType(typeof(TSpan)))
            .Cast<TSpan>()
            .ToArray();
    }
}

// usage
ISpanned ss = ...;
var spans = ss.GetSpans<SyleSpan>(0, 20);

这篇关于Xamarin:确定GetSpans()中的元素是粗体还是斜体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 07:58