I have written a Windows Store App that I need to port to Android. I am attempting to use MvvmCross and Xamarin in Visual Studio to achieve this. In my Windows App, I would create a screen using XAML and in the textbox etc. set the binding to the field in my datamodel object. I would get my datamodel objects from a WCF service reference. In the code behind for the screen, I would just set the datacontext of the root layout grid to the datamodel object generated by the Service Reference. It was pretty simple.
In MvvmCross, it seems that you basically run the viewmodel in order to load a page. The syntax for the fields in the viewmodel are really identical to the ones generated in the datamodel by the service reference. I know that Mvvm needs the viewmodel as the shim between the datamodel and the view. Is there an efficient way to pass the properties from the datamodel, through the viewmodel to the view? I have the service reference working and generating objects and data from the WCF. I could hard code each field that exists in the datamodel into the viewmodel and have the get set act on fields from the datamodel object. I was just hoping there was a less manual way to do it. Any suggestions?
@Stuart had an excellent suggestion. Here is what I did. Here is my 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); }
}
}
I pass it a Guid ID and in the Init method, it gets the Shipment Inventory and the Associated Shipment. When I bind the fields, I just bind to Shipment. as follows:
<?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>
That's all there was to it!
Hope this helps someone.
Jim