This is my HomePage.xaml in this I have one ListView and it is binding from model using WebApi but I debugged everything is on track even data are coming from api and added in list view but when page is coming blank.
HomePage.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="CookBook.HomePage"
xmlns:local="clr-namespace:CookBook"
xmlns:controls="clr-namespace:ImageCircle.Forms.Plugin.Abstractions;assembly=ImageCircle.Forms.Plugin"
Title="Start Exploring">
<ContentPage.BindingContext>
<local:RecpViewModel/>
</ContentPage.BindingContext>
<ContentPage.ToolbarItems>
<ToolbarItem Text="Add"/>
</ContentPage.ToolbarItems>
<ContentPage.Content>
<StackLayout>
<StackLayout Margin="10,15,10,15">
<Frame CornerRadius="100"
HeightRequest="100"
WidthRequest="100"
HorizontalOptions="Center"
Padding="0"
IsClippedToBounds="True" BackgroundColor="Black">
<Image Source="Avatar02.png"
HorizontalOptions="Center"
VerticalOptions="Center"/>
</Frame>
<Frame CornerRadius="20">
<Label x:Name="lblLoginUserName" TextColor="#2B547E" FontAttributes="Bold" HorizontalOptions="Center"></Label>
</Frame>
</StackLayout>
<ListView ItemsSource="{Binding Itemlist}"
HasUnevenRows="True"
SeparatorColor="DeepSkyBlue"
SeparatorVisibility="None"
IsPullToRefreshEnabled="True"
RefreshControlColor="SlateGray">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<ViewCell.ContextActions>
<MenuItem Text="Favourite"></MenuItem>
</ViewCell.ContextActions>
<Grid Padding="10">
<Frame CornerRadius="10" HasShadow="True">
<StackLayout Orientation="Horizontal">
<Image Source="{Binding RImage}" WidthRequest="50" HeightRequest="50"></Image>
<StackLayout VerticalOptions="Center">
<Label VerticalOptions="Center" Text="{Binding Name}" FontSize="Large"></Label>
<Label VerticalOptions="Center" Text="{Binding Detail}" FontSize="Large"></Label>
</StackLayout>
<StackLayout VerticalOptions="Center" HorizontalOptions="EndAndExpand">
<Label Text="Edit" FontSize="Small"></Label>
<Label Text="Delete" FontSize="Small"></Label>
</StackLayout>
</StackLayout>
</Frame>
</Grid>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
</ContentPage.Content>
</ContentPage>
HomePage.xaml.cs
using CookBook.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
namespace CookBook
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class HomePage : ContentPage
{
public HomePage(string UserName)
{
InitializeComponent();
lblLoginUserName.Text = "Welcome To CookBook" + " " + UserName;
}
}
}
LoginService.cs for getting data from database using web api
using CookBook.Model;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
namespace CookBook.Services
{
public class LoginService : ILoginRepository
{
public async Task<List<MyListModel>> CBookList()
{
var baseAddr = new Uri("http://xamarinwebapi.somee.com");
var client = new HttpClient { BaseAddress = baseAddr };
var returnedJson = await client.GetStringAsync("/api/UserMst/GetRecpList");
var result = JsonConvert.DeserializeObject<List<MyListModel>>(returnedJson);
if (result != null)
{
return await Task.FromResult<List<MyListModel>>(result);
}
else
{
return null;
}
}
}
}
RecpViewModel.cs page for binding ListView with data coming from api
using CookBook.Services;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;
using System.Dynamic;
namespace CookBook
{
public class RecpViewModel
{
public ObservableCollection<MyListModel> Itemlist { get; set; }
readonly ILoginRepository loginRepository = new LoginService();
public RecpViewModel()
{
RecpViewModelChild();
}
public async void RecpViewModelChild()
{
List<MyListModel> RecpList = await loginRepository.CBookList();
Itemlist = new ObservableCollection<MyListModel>();
foreach (var item in (dynamic)RecpList)
{
Itemlist.Add(new MyListModel
{
Name = item.Name,
Detail = item.Detail,
RImage = item.RImage,
Ingredients = item.Ingredients
});
}
}
}
}
MyListModel.cs ----Model for my table
using System;
using System.Collections.Generic;
using System.Text;
namespace CookBook
{
public class MyListModel
{
public string Name { get; set; }
public string Detail { get; set; }
public string RImage { get; set; }
public string Ingredients { get; set; }
}
}