问题: - local:MvxLang在使用MVVMCross中的本地化功能时,它是Xamarin应用程序中MvxListView的itemtemplate的一部分时,无法将资源文本绑定到TextView。
我的应用程序是Xamarin.android,使用MVVMCross,使用MVVMCross的本地化功能和resx文件。 可在此处找到运行示例:https://github.com/pallaviak1/RestaurantBilling.Droid
我在viewmodel(AllBillsViewModel)BillClickedCommand中使用以下语法获取本地化的字符串代码: -
_dialogService.ShowAlertAsync( string.Format(TextSource.GetText("InformationReceivedMessage"), bill.CustomerEmail, bill.AmountPaid), TextSource.GetText("InformationReceivedHeader"), TextSource.GetText("InformationReceivedButtonText"));
我的主视图页面中local:mvxLang是按钮属性,它显示来自所选文化资源的按钮文本,也很有效。
<Button ... local:MvxLang="Text ViewBillsResourceText" local:MvxBind="Click NavigateAllBills" />
问题: - 但是,当我在MvxList控件的项模板的控件中使用MvxLang时,本地化字符串为空(未填充)。
档案::RestaurantBilling.Droid \ RestaurantBilling.Droid \ Resources \ layout \ ListItem_Bill.axml
作为项目模板视图一部分的控件如下所示: -
<TextView android:layout_alignParentBottom="true"
android:layout_marginLeft="50dp"
android:layout_marginRight="16dp"
android:layout_width="90dp"
android:layout_height="wrap_content"
local:MvxLang="Text CustomerEmailTextView" />
邮件列表控件如下所示: -
<Mvx.MvxListView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
local:MvxBind="ItemsSource AllBills; ItemClick BillClickedCommand"
local:MvxItemTemplate="@layout/listitem_bill" />
CustomerEmailTextView资源键在resx文件中显示为Name:AllBillsViewModel.CustomerEmailTextView,值:“US Customer Email”。
添加,我的本地化代码如下: - 资源文件存在于库MVVMCross.Localization中,其参考文献已添加到RestaurantBilling.core库以及android项目中。
核心代码,App.cs文件如下: -
Mvx.RegisterSingleton<IMvxTextProvider>
(new ResxTextProvider(Strings.ResourceManager, currentCulture));
BaseViewModel的代码如下: -
public IMvxLanguageBinder TextSource =>
new MvxLanguageBinder("", GetType().Name);
在visual studio输出窗口中获取警告,如下所示:
[0:] MvxBind:Warning: 9.78 Unable to bind: source property source not found Property:TextSource on Bill
02-10 07:41:52.020 I/MvxBind ( 4357): 9.78 Unable to bind: source property source not found Property:TextSource on Bill
我在MVVMCross正式网站上找不到足够的帮助,也找不到相同的讨论点。同样的事情是从多元化培训中提到的样本下载“我的火车”。
请帮忙。
答案 0 :(得分:2)
我的问题已解决。参考链接:How to bind ItemClick in MvxListView in MvxListView
此链接问题有些类似。我以下面的方式解决了这个问题,
我在viewModel中添加的任何TextSource属性,我需要放入Bill.cs类,这是模型
public IMvxLanguageBinder TextSource
{
get {
//Mvx.Trace("****************TextSource get in bill.cs**************************");
return new MvxLanguageBinder("", GetType().Name);
}
}
然后,资源文本键如下所示: -
Bill.CustomerEmailTextView
这是一种解决方法,实际上BaseViewModel具有TextSource属性,但在项目模板的情况下它没有用处。
如果你有更好的解决方案,请告诉我,因为我们有点混合viewmodel和模型属性。