MvvmCross Binding 你的 DataModel 到你的 ViewModel

MvvmCross Binding your DataModel to your ViewModel

我已经编写了一个 Windows 商店应用程序,我需要移植到 Android。我试图在 Visual Studio 中使用 MvvmCross 和 Xamarin 来实现这一点。在我的 Windows 应用程序中,我将使用 XAML 创建一个屏幕,并在文本框等中设置绑定到我的数据模型对象中的字段。我将从 WCF 服务引用中获取我的数据模型对象。在屏幕后面的代码中,我只是将根布局网格的数据上下文设置为服务引用生成的数据模型对象。很简单。

在 MvvmCross 中,您基本上 运行 视图模型似乎是为了加载页面。视图模型中字段的语法与服务引用在数据模型中生成的语法完全相同。我知道 Mvvm 需要视图模型作为数据模型和视图之间的垫片。有没有一种有效的方法可以将属性从数据模型通过视图模型传递到视图?我有服务参考工作并从 WCF 生成对象和数据。我可以将数据模型中存在的每个字段硬编码到视图模型中,并让 get set 作用于数据模型对象中的字段。我只是希望有一种更少的手动方式来做到这一点。有什么建议吗?

@Stuart 有一个很好的建议。这是我所做的。这是我的视图模型:

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 方法中,它获取装运库存和关联的装运。当我绑定字段时,我只绑定到 Shipment。如下:

<?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>

仅此而已!

希望这对某人有所帮助。

吉姆