Bindable Picker Using MVVM and Azure Easy Tables

nialmcshanenialmcshane Member ✭✭
edited June 2018 in Xamarin.Forms

Hi Guys,

I am breaking myself into the MVVM pattern as well as using Azure Mobile Apps.
I have managed to get my login system working and querying the database for salt/hash password.

Next simple thing I need to do is populate a dropdown from a query but I am hitting the wall again - here is what I have so far.

MODEL

public class tbl_DeliveryRuns
{

    string _id;
    string _description;
    string _addedByID;
    bool _active;

    [JsonProperty(PropertyName = "id")]
    public string Id
    {
        get { return _id; }
        set { _id = value; }
    }

    [JsonProperty(PropertyName = "description")]
    public string Description
    {
        get { return _description; }
        set { _description = value; }
    }

    [JsonProperty(PropertyName = "active")]
    public bool Active
    {
        get { return _active; }
        set { _active = value; }
    }

    [JsonProperty(PropertyName = "addedByID")]
    public string AddedByID
    {
        get { return _addedByID; }
        set { _addedByID = value; }
    }

    [Version]
    public string Version { get; set; }

}

VIEW MODEL

using demo.Models;
using Microsoft.WindowsAzure.MobileServices;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;

namespace demo.ViewModels
{
public class ViewModelToDoItem : ViewModelBase
{

    IMobileServiceTable<tbl_DeliveryRuns> deliveryRunTable;
    MobileServiceClient client;


    public List<tbl_DeliveryRuns> DelivieryRunList { get; set; }

    public ViewModelToDoItem()
    {

        client = new MobileServiceClient(Constants.ApplicationURL);
        deliveryRunTable = client.GetTable<tbl_DeliveryRuns>();
        DelivieryRunList = GetDeliveryRuns().OrderBy(t => t.Description).ToList();
    }


    public List<tbl_DeliveryRuns> GetDeliveryRuns()
    {
        var deliveryRuns = new List<tbl_DeliveryRuns>()
        {

    //THIS IS WHERE I NEED TO DO THE AZURE DB CALL AND ASSIGN RESULTS TO A LIST

            new tbl_DeliveryRuns(){Id =  "123456789", AddedByID= "nial", Active=true, Description="Warrenpoint"},
            new tbl_DeliveryRuns(){Id =  "123456789", AddedByID= "nial", Active=true, Description="Dungannon"},

        };

        return deliveryRuns;
    }

    bool _isBusy;


    private string _myRun;
    public string MyRun
    {
        get { return _myRun; }
        set
        {
            if (_myRun != value)
            {
                _myRun = value;
                OnPropertyChanged();
            }
        }
    }

    private tbl_DeliveryRuns _selectedRun { get; set; }
    public tbl_DeliveryRuns SelectedRun
    {
        get { return _selectedRun; }
        set
        {
            if (_selectedRun != value)
            {
                _selectedRun = value;
                // Do whatever functionality you want...When a selectedItem is changed..
                // write code here..
                MyRun = "Selected Run : " + _selectedRun.Id;
            }
        }

    }

    public bool IsBusy
    {

        get { return _isBusy; }
        set { _isBusy = value;
            OnPropertyChanged();
            SaveContactCommand.ChangeCanExecute();
            LaunchWebsiteCommand.ChangeCanExecute();
        }

    }

}

}

Your help would be greatly appreciated - I believe I have to perform the database read and return the results as a list.

Thanks,

Sign In or Register to comment.