Reputation: 1127
Good Day. I'm having trouble on how am I going to show all the records I have created in my ASP.NET WEB API to Xamarin.Forms Application. I tried creating pre-defined list of Employee's Name and Department and it worked. But what I want to do is Create a Record in ASP.NET Web Application and make it appear to my mobile application. Any help will be highly appreciated. Thanks in advance. I watch a video tutorial regarding this matter. Refer to this link if needed. (https://www.youtube.com/watch?v=Lir75oNAeiM&index=2&list=PLpbcUe4chE7-uGCH1S0-qeuCWOMa2Tmam) Here's my code.
MainPageMain.xaml
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="XamarinDemo.Views.MainPageMain"
xmlns:ViewModels="clr-namespace:XamarinDemo.ViewModels;assembly=XamarinDemo"
BackgroundColor="Teal">
<ContentPage.BindingContext>
<ViewModels:MainViewModel/>
</ContentPage.BindingContext>
<ListView ItemsSource="{Binding EmployeesList}"
HasUnevenRows="True">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout Orientation="Vertical"
Padding="12,6">
<Label Text="{Binding Name}"
FontSize="24"/>
<Label Text="{Binding Department}"
FontSize="18"
Opacity="0.6"/>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</ContentPage>
MainViewModel.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using XamarinFormsDemo.Models;
using XamarinFormsDemo.Services;
namespace XamarinFormsDemo.ViewModels
{
public class MainViewModel : INotifyPropertyChanged
{
private List<Employee> _employeesList;
public List<Employee> EmployeesList
{
get { return _employeesList; }
set
{
_employeesList = value;
OnPropertyChanged();
}
}
public MainViewModel()
{
InitializeDataAsync();
}
private async Task InitializeDataAsync()
{
var employeesServices = new EmployeesServices();
EmployeesList = await employeesServices.GetEmployeesAsync();
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
var handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
EmployeesServices.cs
using Plugin.RestClient;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XamarinFormsDemo.Models;
namespace XamarinFormsDemo.Services
{
public class EmployeesServices
{
public async Task<List<Employee>> GetEmployeesAsync()
{
RestClient<Employee> restClient = new RestClient<Employee>();
var employeesList = await restClient.GetAsync();
return employeesList;
}
}
}
Upvotes: 1
Views: 448
Reputation: 4032
Here is a helpful post about the correct way to do this:
http://arteksoftware.com/end-to-end-mvvm-with-xamarin/
If your records are not being displayed completely on your mobile app you can test your ASP web service using POSTMAN and double check if it is working correctly and successfully returning the correct data, then bind the List or Observable collection correctly on your View Model and then update the list/Observable Collection property properly in order to show the latest records.
Upvotes: 1