在ViewModel - MvvmCross中获取String.xml值(Get String.xml values in the ViewModel - MvvmCross)

我在viewModel有以下列表。 但是为了支持多种语言,我希望这个字符串来自string.xml 。 可能吗?

private List<Thing> _items = new List<Thing>() { new Thing("Open"), new Thing("Close"), };

I have the following list in my viewModel. However in order to support multiple language, I want this string to come from string.xml. Is it possible?

private List<Thing> _items = new List<Thing>() { new Thing("Open"), new Thing("Close"), };

最满意答案

如果您正在构建原生Android和iOS应用程序,则可以使用该界面:

public interface ILocalization { string GetLocalizedString(string key); }

在Android和iOS项目中,您有一个实现接口Android的类:

public class LocalizationAndroid : ILocalization { public Activity context; public LocalizationAndroid(Activity _context) { context = _context; } public string GetLocalizedString(string key) { var resourceId = (int)typeof(Resource.String).GetField(key).GetValue(null); return context.GetString(resourceId); } }

iOS版:

public class LocalizationIOS : ILocalization { public string GetLocalizedString(string key) { return NSBundle.MainBundle.LocalizedString(key, null); } }

然后将该类的一个实例(在您的视图中实例化)传递给视图模型以获取字符串。 这些字符串必须放置在iOS项目的Localizable.strings文件和Android项目的Strings.xml文件中。

在ViewModel上,您将拥有:

public class MyViewModel { public ILocalization LocalizationObject; private List<Thing> _items; public MyViewModel(ILocalization _localizationObject) { LocalizationObject = _localizationObject; _items.Add(new Thing(LocalizationObject.GetLocalizedString("Open"))); _items.Add(new Thing(LocalizationObject.GetLocalizedString("Closed"))); } }

考虑到您为Strings.xml (Android)和Localizable.strings (iOS)上的“Open”和“Closed”键添加了两个字符串“Open”和“Closed”。

If you are building native android and iOS apps you could have the interface:

public interface ILocalization { string GetLocalizedString(string key); }

And in the Android and iOS projects you have a class that implements the interface, Android:

public class LocalizationAndroid : ILocalization { public Activity context; public LocalizationAndroid(Activity _context) { context = _context; } public string GetLocalizedString(string key) { var resourceId = (int)typeof(Resource.String).GetField(key).GetValue(null); return context.GetString(resourceId); } }

iOS:

public class LocalizationIOS : ILocalization { public string GetLocalizedString(string key) { return NSBundle.MainBundle.LocalizedString(key, null); } }

And then pass one instance of the class (instantiated in your view) to the view model to get the strings. The strings must be placed at the Localizable.strings file for the iOS project and at the Strings.xml file for the Android project.

On the ViewModel you would have:

public class MyViewModel { public ILocalization LocalizationObject; private List<Thing> _items; public MyViewModel(ILocalization _localizationObject) { LocalizationObject = _localizationObject; _items.Add(new Thing(LocalizationObject.GetLocalizedString("Open"))); _items.Add(new Thing(LocalizationObject.GetLocalizedString("Closed"))); } }

This, considering the fact that you added the two strings "Open" and "Closed" for the "Open" and "Closed" keys on the Strings.xml(Android) and the Localizable.strings (iOS).

更多推荐