MvvmCross动态文本值转换 [英] MvvmCross Dynamic Text Value Conversion

查看:68
本文介绍了MvvmCross动态文本值转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,MvvmCross本地化插件提供了静态"引擎.我以会议中的以下绑定为例:

As far as I know, MvvmCross localization plugin provides "static" engine. I use the following binding as an example from Conference:

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

我希望能够动态地将SQLBitsXApp更改为SQLBitsXApp2. 目的是找到与天枚举有关的本地化文本.

I want to be able to change SQLBitsXApp to SQLBitsXApp2 dynamically. The goal is to find the localized text related to days enum.

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

Is there a way to do this dynamically ?

推荐答案

您是正确的-该绑定中使用的默认MvxLanguageConverter实际上仅适用于简单的静态文本.

You're correct - the default MvxLanguageConverter used in that binding is really there only for simple static text.

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

For more involved situations you will need to build your own converter for each case - but hopefully some of these will be reusable.

作为开始的示例,使用

As a starting example, check out how the Conference sample displays tweet times using 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中使用,例如

This is used in Android axml like in 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'}}"
   />

这篇关于MvvmCross动态文本值转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆