MvvmCross动态文本值转换

2024-01-11

据我所知,MvvmCross 本地化插件提供“静态”引擎。我使用以下来自 Conference 的绑定作为示例:

local:MvxBind="{'Text'{'Path':'TextSource','Converter':'Language','ConverterParameter':'SQLBitsXApp'}}"

我希望能够改变SQLBitsXApp to SQLBitsXApp2动态地。 目标是找到与天枚举相关的本地化文本。

有没有办法动态地做到这一点?


你是对的 - 该绑定中使用的默认 MvxLanguageConverter 实际上仅适用于简单的静态文本。

对于更复杂的情况,您将需要为每种情况构建自己的转换器 - 但希望其中一些可以重复使用。

作为起始示例,请查看会议示例如何使用以下命令显示推文时间TimeAgoConverter.cs https://github.com/slodge/MvvmCross/blob/vnext/Sample%20-%20CirriousConference/Cirrious.Conference.Core/Converters/TimeAgoConverter.cs

public class TimeAgoValueConverter
    : MvxBaseValueConverter
      , IMvxServiceConsumer<IMvxTextProvider>
{
    private IMvxTextProvider _textProvider;
    private IMvxTextProvider TextProvider
    {
        get
        {
            if (_textProvider == null)
            {
                _textProvider = this.GetService<IMvxTextProvider>();
            }
            return _textProvider;
        }
    }

    public override object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        var when = (DateTime)value;

        string whichFormat;
        int valueToFormat;

        if (when == DateTime.MinValue)
        {
            whichFormat = "TimeAgo.Never";
            valueToFormat = 0;
        }
        else
        {
            var whenUtc = when.ToUniversalTime();
            var difference = (DateTime.UtcNow - whenUtc).TotalSeconds;
            if (difference < 30.0)
            {
                whichFormat = "TimeAgo.JustNow";
               valueToFormat = 0;
            }
            // ... etc
        }

        var format = TextProvider.GetText(Constants.GeneralNamespace, Constants.Shared, whichFormat);
        return string.Format(format, valueToFormat);
    }
}

这在 Android axml 中使用,如https://github.com/slodge/MvvmCross/blob/vnext/Sample%20-%20CirriousConference/Cirrious.Conference.UI.Droid/Resources/Layout/ListItem_Tweet.xml https://github.com/slodge/MvvmCross/blob/vnext/Sample%20-%20CirriousConference/Cirrious.Conference.UI.Droid/Resources/Layout/ListItem_Tweet.xml:

<TextView
 android:id="@+id/TimeTextView"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:textSize="10dip"
 android:textColor="@color/icongrey"
  local:MvxBind="{'Text':{'Path':'Item.Timestamp','Converter':'TimeAgo'}}"
   />
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

MvvmCross动态文本值转换 的相关文章

随机推荐