1 回答

TA貢獻(xiàn)1891條經(jīng)驗(yàn) 獲得超3個(gè)贊
如果您使用的是 Xamarin Forms,則可以在您的核心項(xiàng)目中創(chuàng)建 resx 文件,例如 AppResources.resx 和 AppResources.es.resx 并在其中放置一個(gè)簡(jiǎn)單的文本,例如:
在 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)建一個(gè)類,名稱類似于從 MvxViewModel 擴(kuò)展的 BaseViewModel,并在其中引用本地化字符串的索引:
public class BaseViewModel : MvxViewModel
{
public BaseViewModel()
{
}
public string this[string index] => AppResources.ResourceManager.GetString(index);
}
現(xiàn)在從該 BaseViewModel 擴(kuò)展所有視圖模型。然后在來(lái)自 xamarin 表單的 xaml 文件中,當(dāng)您創(chuàng)建一個(gè)內(nèi)容頁(yè)面時(shí),將其設(shè)為 MvxContentPage 并且您可以通過(guò)它附加一個(gè)視圖模型x:TypeArguments,那里有對(duì) AppResources 索引的引用,所以如果想使用來(lái)自本地化 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 項(xiàng)目以查看有多少東西(如本地化字符串)有效;)
- 1 回答
- 0 關(guān)注
- 94 瀏覽
添加回答
舉報(bào)