1 回答

TA貢獻1891條經(jīng)驗 獲得超3個贊
如果您使用的是 Xamarin Forms,則可以在您的核心項目中創(chuàng)建 resx 文件,例如 AppResources.resx 和 AppResources.es.resx 并在其中放置一個簡單的文本,例如:
在 AppResources.resx
<data name="Hello" xml:space="preserve">
<value>Hello</value>
</data>
并在 AppResources.es.resx
<data name="Hello" xml:space="preserve">
<value>Hola</value>
</data>
然后創(chuàng)建一個類,名稱類似于從 MvxViewModel 擴展的 BaseViewModel,并在其中引用本地化字符串的索引:
public class BaseViewModel : MvxViewModel
{
public BaseViewModel()
{
}
public string this[string index] => AppResources.ResourceManager.GetString(index);
}
現(xiàn)在從該 BaseViewModel 擴展所有視圖模型。然后在來自 xamarin 表單的 xaml 文件中,當(dāng)您創(chuàng)建一個內(nèi)容頁面時,將其設(shè)為 MvxContentPage 并且您可以通過它附加一個視圖模型x:TypeArguments,那里有對 AppResources 索引的引用,所以如果想使用來自本地化 resx 的字符串,只需使用常規(guī)的 xamarin 表單綁定并將字符串名稱作為索引傳遞,例如:
<Label
FontSize="Medium"
TextColor="Black"
Text="{Binding [Hello]}" />
或者使用 mvx 綁定系統(tǒng)
<Label
FontSize="Medium"
TextColor="Black"
mvx:Bi.nd="Text [Hello]" />
您可以檢查Star Wars Sample 項目以查看有多少東西(如本地化字符串)有效;)
- 1 回答
- 0 關(guān)注
- 86 瀏覽
添加回答
舉報