我已经编写了一个Windows应用程序,我需要移植到Android。我试图在Visual中使用MvvmCross和Xamarin来实现这一点。在我的Windows应用程序中,我将使用XAML创建一个屏幕,在textbox等中设置绑定到数据模型对象中的字段。我将从WCF服务引用中获取数据模型对象。在屏幕后面的代码中,我只需将根布局网格的数据文本设置为由服务引用生成的datamodel对象。很简单。
在MvvmCross中,您似乎基本上运行视图模型是为了加载页面。视图模型中字段的语法实际上与服务引用在数据模型中生成的语法完全相同。我知道Mvvm需要视图模型作为数据模型和视图之间的间隔。是否有一种有效的方法将属性从datamodel传递到视图模型?我有服务参考,工作和从WCF生成对象和数据。我可以将数据模型中存在的每个字段硬编码到视图模型中,并让get set对来自datamodel对象的字段进行操作。我只是希望有一种少一点手工操作的方法。有什么建议吗?
发布于 2015-03-04 16:18:28
斯图尔特有一个很好的建议。这就是我所做的。这是我的ViewModel:
public class InventoryViewModel
: MvxViewModel
{
public async void Init(Guid ID)
{
await MPS_Mobile_Driver.Droid.DataModel.ShipmentDataSource.GetShipmentInventory(ID);
ShipmentInventory = ShipmentDataSource.CurrInventory;
Shipment = await MPS_Mobile_Driver.Droid.DataModel.ShipmentDataSource.GetShipment((int)ShipmentInventory.idno, (short)ShipmentInventory.idsub);
}
private Shipment _Shipment;
public Shipment Shipment
{
get { return _Shipment; }
set { _Shipment = value; RaisePropertyChanged(() => Shipment); }
}
private ShipmentInventory _ShipmentInventory;
public ShipmentInventory ShipmentInventory
{
get { return _ShipmentInventory; }
set { _ShipmentInventory = value; RaisePropertyChanged(() => ShipmentInventory); }
}
}我传递给它一个Guid ID,在Init方法中,它得到货件库存和相关的货件。当我绑定字段时,我只是绑定到货物。如下所示:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
style="@style/InputEditText"
local:MvxBind="Text Shipment.OrgEmail" />
</LinearLayout>就这样了!
希望这能帮上忙。
吉姆
https://stackoverflow.com/questions/28798209
复制相似问题