Skip to content

Commit 213be9a

Browse files
authored
Committing ListView Sample with WebAPI
1 parent 2695fef commit 213be9a

File tree

9 files changed

+630
-0
lines changed

9 files changed

+630
-0
lines changed

SfListViewSample.sln

Lines changed: 337 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<Application xmlns="http://xamarin.com/schemas/2014/forms"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
4+
x:Class="SfListViewSample.App">
5+
<Application.Resources>
6+
7+
</Application.Resources>
8+
</Application>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using System;
2+
using Xamarin.Forms;
3+
using Xamarin.Forms.Xaml;
4+
5+
[assembly: XamlCompilation (XamlCompilationOptions.Compile)]
6+
namespace SfListViewSample
7+
{
8+
public partial class App : Application
9+
{
10+
public App ()
11+
{
12+
InitializeComponent();
13+
14+
MainPage = new MainPage();
15+
}
16+
17+
protected override void OnStart ()
18+
{
19+
// Handle when your app starts
20+
}
21+
22+
protected override void OnSleep ()
23+
{
24+
// Handle when your app sleeps
25+
}
26+
27+
protected override void OnResume ()
28+
{
29+
// Handle when your app resumes
30+
}
31+
}
32+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
using Newtonsoft.Json;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Collections.ObjectModel;
5+
using System.Text;
6+
7+
namespace SfListViewSample
8+
{
9+
/// <summary>
10+
/// Implementation of RestService to be displayed.
11+
/// </summary>
12+
public class RestService : IRestService
13+
{
14+
#region Fields
15+
16+
System.Net.Http.HttpClient client;
17+
18+
#endregion
19+
20+
#region Properties
21+
22+
public ObservableCollection<Order> Items
23+
{
24+
get; private set;
25+
}
26+
27+
public string RestUrl
28+
{
29+
get; private set;
30+
}
31+
32+
#endregion
33+
34+
#region Constructor
35+
public RestService()
36+
{
37+
client = new System.Net.Http.HttpClient();
38+
}
39+
40+
#endregion
41+
42+
#region Methods
43+
public async System.Threading.Tasks.Task<ObservableCollection<Order>> RefreshDataAsync()
44+
{
45+
RestUrl = "https://ej2services.syncfusion.com/production/web-services/api/Orders"; // Set your REST API url here
46+
var uri = new Uri(RestUrl);
47+
try
48+
{
49+
var response = await client.GetAsync(uri);
50+
51+
if (response.IsSuccessStatusCode)
52+
{
53+
var content = await response.Content.ReadAsStringAsync();
54+
Items = JsonConvert.DeserializeObject<ObservableCollection<Order>>(content);
55+
return Items;
56+
}
57+
}
58+
catch (Exception ex)
59+
{
60+
}
61+
return null;
62+
}
63+
64+
System.Threading.Tasks.Task<Order> IRestService.RefreshDataAsync()
65+
{
66+
throw new NotImplementedException();
67+
}
68+
69+
#endregion
70+
}
71+
72+
public interface IRestService
73+
{
74+
System.Threading.Tasks.Task<Order> RefreshDataAsync();
75+
}
76+
77+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
4+
xmlns:local="clr-namespace:SfListViewSample"
5+
xmlns:syncfusion="clr-namespace:Syncfusion.ListView.XForms;assembly=Syncfusion.SfListView.XForms"
6+
x:Class="SfListViewSample.MainPage" Padding="10">
7+
<ContentPage.BindingContext>
8+
<local:OrdersViewModel x:Name="viewmodel"/>
9+
</ContentPage.BindingContext>
10+
11+
<Grid >
12+
<Grid.RowDefinitions>
13+
<RowDefinition Height="50"/>
14+
<RowDefinition Height="*"/>
15+
</Grid.RowDefinitions>
16+
<Label Text="Fetched data from REST Api" BackgroundColor="SlateBlue" FontSize="18" FontAttributes="Bold" TextColor="White" VerticalTextAlignment="Center" HorizontalTextAlignment="Center"/>
17+
<syncfusion:SfListView x:Name="listView" ItemSize="90" ItemSpacing="5" Grid.Row="1"
18+
BackgroundColor="AliceBlue" ItemsSource="{Binding Items}">
19+
<syncfusion:SfListView.ItemTemplate>
20+
<DataTemplate>
21+
<Frame BorderColor="#757575" Padding="5">
22+
<Grid>
23+
<Grid.ColumnDefinitions>
24+
<ColumnDefinition Width="*"/>
25+
<ColumnDefinition Width="*"/>
26+
</Grid.ColumnDefinitions>
27+
<Grid.RowDefinitions>
28+
<RowDefinition Height="Auto"/>
29+
<RowDefinition Height="Auto"/>
30+
<RowDefinition Height="Auto"/>
31+
</Grid.RowDefinitions>
32+
<Label Grid.Row="0" Grid.Column="0" Text="Order Id " HorizontalOptions="Start" TextColor="Black" FontSize="16" FontAttributes="Bold"/>
33+
<Label Grid.Row="1" Grid.Column="0" Text="Customer Id " HorizontalOptions="Start" TextColor="Black" FontSize="16" FontAttributes="Bold"/>
34+
<Label Grid.Row="2" Grid.Column="0" Text="Ship Country " HorizontalOptions="Start" TextColor="Black" FontSize="16" FontAttributes="Bold"/>
35+
36+
<Label Grid.Row="0" Grid.Column="1" Text="{Binding OrderID}" HorizontalOptions="Start" TextColor="Black" FontSize="16" WidthRequest="100"/>
37+
<Label Grid.Row="1" Grid.Column="1" Text="{Binding CustomerID}" HorizontalOptions="Start" TextColor="Black" WidthRequest="100"/>
38+
<Label Grid.Row="2" Grid.Column="1" Text="{Binding ShipCountry}" HorizontalOptions="Start" TextColor="Black" WidthRequest="100"/>
39+
</Grid>
40+
</Frame>
41+
</DataTemplate>
42+
</syncfusion:SfListView.ItemTemplate>
43+
</syncfusion:SfListView>
44+
45+
</Grid>
46+
</ContentPage>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using Newtonsoft.Json;
2+
using Syncfusion.DataSource;
3+
using Syncfusion.ListView.XForms;
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Collections.ObjectModel;
7+
using System.Linq;
8+
using System.Net.Http;
9+
using System.Text;
10+
using System.Threading.Tasks;
11+
using Xamarin.Forms;
12+
13+
namespace SfListViewSample
14+
{
15+
public partial class MainPage : ContentPage
16+
{
17+
#region Constructor
18+
public MainPage()
19+
{
20+
InitializeComponent();
21+
}
22+
#endregion
23+
}
24+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.ComponentModel;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
using Xamarin.Forms;
8+
9+
namespace SfListViewSample
10+
{
11+
public class Order
12+
{
13+
public int OrderID { get; set; }
14+
public string CustomerID { get; set; }
15+
public int EmployeeID { get; set; }
16+
public double Freight { get; set; }
17+
public string ShipCity { get; set; }
18+
public bool Verified { get; set; }
19+
public DateTime OrderDate { get; set; }
20+
public string ShipName { get; set; }
21+
public string ShipCountry { get; set; }
22+
public DateTime ShippedDate { get; set; }
23+
public string ShipAddress { get; set; }
24+
}
25+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netstandard2.0</TargetFramework>
5+
</PropertyGroup>
6+
7+
<ItemGroup>
8+
<PackageReference Include="Newtonsoft.Json" Version="11.0.2" />
9+
<PackageReference Include="Syncfusion.Xamarin.SfListView" Version="17.1.0.43" />
10+
<PackageReference Include="Xamarin.Forms" Version="3.6.0.344457" />
11+
</ItemGroup>
12+
13+
<ItemGroup>
14+
<None Update="App.xaml">
15+
<Generator>MSBuild:Compile</Generator>
16+
</None>
17+
<None Update="MainPage.xaml">
18+
<Generator>MSBuild:Compile</Generator>
19+
</None>
20+
</ItemGroup>
21+
</Project>
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Collections.ObjectModel;
4+
using System.ComponentModel;
5+
using System.Linq;
6+
using System.Net.Http;
7+
using System.Text;
8+
using System.Threading.Tasks;
9+
using Xamarin.Forms;
10+
11+
namespace SfListViewSample
12+
{
13+
public class OrdersViewModel : INotifyPropertyChanged
14+
{
15+
#region Fields
16+
17+
RestService restService;
18+
public event PropertyChangedEventHandler PropertyChanged;
19+
private ObservableCollection<Order> items;
20+
21+
#endregion
22+
23+
#region Properties
24+
public ObservableCollection<Order> Items
25+
{
26+
get
27+
{
28+
return items;
29+
}
30+
set
31+
{
32+
items = value;
33+
RaisepropertyChanged("Items");
34+
}
35+
}
36+
#endregion
37+
38+
#region Constructor
39+
public OrdersViewModel()
40+
{
41+
restService = new RestService();
42+
//Item source which needs to be displayed on the list view.
43+
items = new ObservableCollection<Order>();
44+
GetData();
45+
}
46+
#endregion
47+
48+
#region Methods
49+
async void GetData()
50+
{
51+
Items = await restService.RefreshDataAsync();
52+
}
53+
void RaisepropertyChanged(string propertyName)
54+
{
55+
if (PropertyChanged != null)
56+
PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
57+
}
58+
#endregion
59+
}
60+
}

0 commit comments

Comments
 (0)