Skip to content

Commit 87a7bba

Browse files
Merge pull request #1 from KarthikRaja26/master
how-to-fetch-and-populate-data-using-ODataV4-in-Xamarin.Forms-datagrid
2 parents 4a1e800 + 73701d6 commit 87a7bba

File tree

96 files changed

+16084
-1
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

96 files changed

+16084
-1
lines changed

README.md

Lines changed: 102 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,103 @@
11
# how-to-fetch-and-populate-data-using-ODataV4-in-Xamarin.Forms-datagrid
2-
how to fetch and populate data using ODataV4 in Xamarin.Forms datagrid
2+
3+
## About the sample
4+
This example illustrates how to fetch and popuplate data using ODataV4 in Xmarin.Forms SfDataGrid.
5+
6+
Simple.OData.Client is a cross-platform client library through which you can fetch and update data online. In, the below sample we fetched the data from online and loaded those data to SfDataGrid by using Simple.OData.Client library.
7+
8+
```C#
9+
public class ResultsPage : ContentPage
10+
{
11+
IEnumerable<Package> packages;
12+
SfDataGrid dataGrid;
13+
ActivityIndicator activityIndicator;
14+
public ResultsPage()
15+
{
16+
Title = "Search Results";
17+
18+
NavigationPage.SetHasNavigationBar(this, true);
19+
20+
var stackLayout = new StackLayout() { VerticalOptions = LayoutOptions.FillAndExpand };
21+
22+
if (Device.OS == TargetPlatform.WinPhone)
23+
{
24+
// WinPhone doesn't have the title showing
25+
stackLayout.Children.Add(new Label { Text = Title, Font = Font.SystemFontOfSize(50) });
26+
}
27+
28+
var searchButton = new Button() { Text = "Get Data" };
29+
searchButton.Clicked += async (sender, e) =>
30+
{
31+
try
32+
{
33+
activityIndicator.IsVisible = true;
34+
packages = await GetPackages();
35+
if (packages != null)
36+
SetSource();
37+
activityIndicator.IsVisible = false;
38+
}
39+
catch(Exception)
40+
{
41+
await DisplayAlert("Error","Connect to the internet and try again..!","OK");
42+
activityIndicator.IsVisible = false;
43+
}
44+
};
45+
46+
var grid = new Grid();
47+
activityIndicator = new ActivityIndicator();
48+
activityIndicator.HeightRequest = 100;
49+
activityIndicator.HorizontalOptions = LayoutOptions.Center;
50+
activityIndicator.VerticalOptions = LayoutOptions.Center;
51+
activityIndicator.IsEnabled = true;
52+
activityIndicator.IsRunning = true;
53+
activityIndicator.IsVisible = false;
54+
55+
dataGrid = new SfDataGrid();
56+
dataGrid.ColumnSizer = ColumnSizer.Auto;
57+
dataGrid.GridTapped += (sender, e) =>
58+
{
59+
var package = (PackageViewModel)e.RowData;
60+
var detailsPage = new DetailsPage();
61+
detailsPage.BindingContext = package;
62+
Navigation.PushAsync(detailsPage);
63+
};
64+
65+
grid.Children.Add(activityIndicator);
66+
grid.Children.Add(dataGrid);
67+
stackLayout.Children.Add(searchButton);
68+
stackLayout.Children.Add(grid);
69+
70+
this.Content = stackLayout;
71+
}
72+
73+
private void SetSource()
74+
{
75+
var results = packages.Select(x => new PackageViewModel(x));
76+
dataGrid.ItemsSource = results;
77+
}
78+
79+
private async Task<IEnumerable<Package>> GetPackages()
80+
{
81+
var odataClient = new ODataClient("https://nuget.org/api/v1");
82+
var command = odataClient
83+
.For<Package>("Packages")
84+
.OrderByDescending(x => x.DownloadCount)
85+
.Top(2);
86+
87+
command.OrderBy(x => x.Id);
88+
command.Filter(x => x.Title.Contains("Xamarin") && x.IsLatestVersion);
89+
command.Select(x => new { x.Id, x.Title, x.Version, x.LastUpdated, x.DownloadCount, x.VersionDownloadCount, x.PackageSize, x.Authors, x.Dependencies });
90+
91+
return await command.FindEntriesAsync();
92+
}
93+
}
94+
```
95+
96+
## <a name="requirements-to-run-the-demo"></a>Requirements to run the demo ##
97+
98+
* [Visual Studio 2017](https://visualstudio.microsoft.com/downloads/) or [Visual Studio for Mac](https://visualstudio.microsoft.com/vs/mac/).
99+
* Xamarin add-ons for Visual Studio (available via the Visual Studio installer).
100+
101+
## <a name="troubleshooting"></a>Troubleshooting ##
102+
### Path too long exception
103+
If you are facing path too long exception when building this example project, close Visual Studio and rename the repository to short and build the project.

SfDataGridDemo/NuGetFinder/NuGetFinder.sln

Lines changed: 337 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Any raw assets you want to be deployed with your application can be placed in
2+
this directory (and child directories) and given a Build Action of "AndroidAsset".
3+
4+
These files will be deployed with you package and will be accessible using Android's
5+
AssetManager, like this:
6+
7+
public class ReadAsset : Activity
8+
{
9+
protected override void OnCreate (Bundle bundle)
10+
{
11+
base.OnCreate (bundle);
12+
13+
InputStream input = Assets.Open ("my_asset.txt");
14+
}
15+
}
16+
17+
Additionally, some Android functions will automatically load asset files:
18+
19+
Typeface tf = Typeface.CreateFromAsset (Context.Assets, "fonts/samplefont.ttf");
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using System;
2+
3+
using Android.App;
4+
using Android.Content.PM;
5+
using Android.Runtime;
6+
using Android.Views;
7+
using Android.Widget;
8+
using Android.OS;
9+
10+
namespace NuGetFinder.Droid
11+
{
12+
[Activity(Label = "NuGetFinder", Icon = "@mipmap/icon", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
13+
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
14+
{
15+
protected override void OnCreate(Bundle savedInstanceState)
16+
{
17+
TabLayoutResource = Resource.Layout.Tabbar;
18+
ToolbarResource = Resource.Layout.Toolbar;
19+
20+
base.OnCreate(savedInstanceState);
21+
global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
22+
LoadApplication(new App());
23+
}
24+
}
25+
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup>
4+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
5+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
6+
<ProjectGuid>{BD3358F6-951E-4DC9-83F6-7D1C52C4598E}</ProjectGuid>
7+
<ProjectTypeGuids>{EFBA0AD7-5A72-4C68-AF49-83D382785DCF};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
8+
<TemplateGuid>{c9e5eea5-ca05-42a1-839b-61506e0a37df}</TemplateGuid>
9+
<OutputType>Library</OutputType>
10+
<RootNamespace>NuGetFinder.Droid</RootNamespace>
11+
<AssemblyName>NuGetFinder.Android</AssemblyName>
12+
<AndroidApplication>True</AndroidApplication>
13+
<AndroidResgenFile>Resources\Resource.designer.cs</AndroidResgenFile>
14+
<AndroidResgenClass>Resource</AndroidResgenClass>
15+
<AndroidManifest>Properties\AndroidManifest.xml</AndroidManifest>
16+
<MonoAndroidResourcePrefix>Resources</MonoAndroidResourcePrefix>
17+
<MonoAndroidAssetsPrefix>Assets</MonoAndroidAssetsPrefix>
18+
<AndroidUseLatestPlatformSdk>false</AndroidUseLatestPlatformSdk>
19+
<TargetFrameworkVersion>v9.0</TargetFrameworkVersion>
20+
<AndroidHttpClientHandlerType>Xamarin.Android.Net.AndroidClientHandler</AndroidHttpClientHandlerType>
21+
<NuGetPackageImportStamp>
22+
</NuGetPackageImportStamp>
23+
</PropertyGroup>
24+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
25+
<DebugSymbols>true</DebugSymbols>
26+
<DebugType>portable</DebugType>
27+
<Optimize>false</Optimize>
28+
<OutputPath>bin\Debug</OutputPath>
29+
<DefineConstants>DEBUG;</DefineConstants>
30+
<ErrorReport>prompt</ErrorReport>
31+
<WarningLevel>4</WarningLevel>
32+
<AndroidLinkMode>None</AndroidLinkMode>
33+
</PropertyGroup>
34+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
35+
<DebugSymbols>true</DebugSymbols>
36+
<DebugType>pdbonly</DebugType>
37+
<Optimize>true</Optimize>
38+
<OutputPath>bin\Release</OutputPath>
39+
<ErrorReport>prompt</ErrorReport>
40+
<WarningLevel>4</WarningLevel>
41+
<AndroidManagedSymbols>true</AndroidManagedSymbols>
42+
<AndroidUseSharedRuntime>false</AndroidUseSharedRuntime>
43+
</PropertyGroup>
44+
<ItemGroup>
45+
<Reference Include="Mono.Android" />
46+
<Reference Include="System" />
47+
<Reference Include="System.Core" />
48+
<Reference Include="System.Xml.Linq" />
49+
<Reference Include="System.Xml" />
50+
</ItemGroup>
51+
<ItemGroup>
52+
<PackageReference Include="Syncfusion.Xamarin.SfDataGrid">
53+
<Version>17.4.0.50</Version>
54+
</PackageReference>
55+
<PackageReference Include="Xamarin.Forms" Version="4.4.0.991640" />
56+
</ItemGroup>
57+
<ItemGroup>
58+
<Compile Include="MainActivity.cs" />
59+
<Compile Include="Resources\Resource.designer.cs" />
60+
<Compile Include="Properties\AssemblyInfo.cs" />
61+
</ItemGroup>
62+
<ItemGroup>
63+
<None Include="Resources\AboutResources.txt" />
64+
<None Include="Assets\AboutAssets.txt" />
65+
<None Include="Properties\AndroidManifest.xml" />
66+
</ItemGroup>
67+
<ItemGroup>
68+
<AndroidResource Include="Resources\layout\Tabbar.axml" />
69+
<AndroidResource Include="Resources\layout\Toolbar.axml" />
70+
<AndroidResource Include="Resources\values\styles.xml" />
71+
<AndroidResource Include="Resources\values\colors.xml" />
72+
<AndroidResource Include="Resources\mipmap-anydpi-v26\icon.xml" />
73+
<AndroidResource Include="Resources\mipmap-anydpi-v26\icon_round.xml" />
74+
<AndroidResource Include="Resources\mipmap-hdpi\icon.png" />
75+
<AndroidResource Include="Resources\mipmap-hdpi\launcher_foreground.png" />
76+
<AndroidResource Include="Resources\mipmap-mdpi\icon.png" />
77+
<AndroidResource Include="Resources\mipmap-mdpi\launcher_foreground.png" />
78+
<AndroidResource Include="Resources\mipmap-xhdpi\icon.png" />
79+
<AndroidResource Include="Resources\mipmap-xhdpi\launcher_foreground.png" />
80+
<AndroidResource Include="Resources\mipmap-xxhdpi\icon.png" />
81+
<AndroidResource Include="Resources\mipmap-xxhdpi\launcher_foreground.png" />
82+
<AndroidResource Include="Resources\mipmap-xxxhdpi\icon.png" />
83+
<AndroidResource Include="Resources\mipmap-xxxhdpi\launcher_foreground.png" />
84+
</ItemGroup>
85+
<ItemGroup>
86+
<Folder Include="Resources\drawable-hdpi\" />
87+
<Folder Include="Resources\drawable-xhdpi\" />
88+
<Folder Include="Resources\drawable-xxhdpi\" />
89+
<Folder Include="Resources\drawable-xxxhdpi\" />
90+
<Folder Include="Resources\drawable\" />
91+
</ItemGroup>
92+
<ItemGroup>
93+
<ProjectReference Include="..\NuGetFinder\NuGetFinder.csproj">
94+
<Project>{D0272261-286B-4A29-9B76-1207849AE0EE}</Project>
95+
<Name>NuGetFinder</Name>
96+
</ProjectReference>
97+
</ItemGroup>
98+
<Import Project="$(MSBuildExtensionsPath)\Xamarin\Android\Xamarin.Android.CSharp.targets" />
99+
</Project>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="com.companyname.NuGetFinder">
3+
<uses-sdk android:minSdkVersion="21" android:targetSdkVersion="28" />
4+
<application android:label="NuGetFinder.Android"></application>
5+
</manifest>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
using Android.App;
5+
6+
// General Information about an assembly is controlled through the following
7+
// set of attributes. Change these attribute values to modify the information
8+
// associated with an assembly.
9+
[assembly: AssemblyTitle("NuGetFinder.Android")]
10+
[assembly: AssemblyDescription("")]
11+
[assembly: AssemblyConfiguration("")]
12+
[assembly: AssemblyCompany("")]
13+
[assembly: AssemblyProduct("NuGetFinder.Android")]
14+
[assembly: AssemblyCopyright("Copyright © 2014")]
15+
[assembly: AssemblyTrademark("")]
16+
[assembly: AssemblyCulture("")]
17+
[assembly: ComVisible(false)]
18+
19+
// Version information for an assembly consists of the following four values:
20+
//
21+
// Major Version
22+
// Minor Version
23+
// Build Number
24+
// Revision
25+
//
26+
// You can specify all the values or you can default the Build and Revision Numbers
27+
// by using the '*' as shown below:
28+
// [assembly: AssemblyVersion("1.0.*")]
29+
[assembly: AssemblyVersion("1.0.0.0")]
30+
[assembly: AssemblyFileVersion("1.0.0.0")]
31+
32+
// Add some common permissions, these can be removed if not needed
33+
[assembly: UsesPermission(Android.Manifest.Permission.Internet)]
34+
[assembly: UsesPermission(Android.Manifest.Permission.WriteExternalStorage)]
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
Images, layout descriptions, binary blobs and string dictionaries can be included
2+
in your application as resource files. Various Android APIs are designed to
3+
operate on the resource IDs instead of dealing with images, strings or binary blobs
4+
directly.
5+
6+
For example, a sample Android app that contains a user interface layout (main.xml),
7+
an internationalization string table (strings.xml) and some icons (drawable-XXX/icon.png)
8+
would keep its resources in the "Resources" directory of the application:
9+
10+
Resources/
11+
drawable-hdpi/
12+
icon.png
13+
14+
drawable-ldpi/
15+
icon.png
16+
17+
drawable-mdpi/
18+
icon.png
19+
20+
layout/
21+
main.xml
22+
23+
values/
24+
strings.xml
25+
26+
In order to get the build system to recognize Android resources, set the build action to
27+
"AndroidResource". The native Android APIs do not operate directly with filenames, but
28+
instead operate on resource IDs. When you compile an Android application that uses resources,
29+
the build system will package the resources for distribution and generate a class called
30+
"Resource" that contains the tokens for each one of the resources included. For example,
31+
for the above Resources layout, this is what the Resource class would expose:
32+
33+
public class Resource {
34+
public class drawable {
35+
public const int icon = 0x123;
36+
}
37+
38+
public class layout {
39+
public const int main = 0x456;
40+
}
41+
42+
public class strings {
43+
public const int first_string = 0xabc;
44+
public const int second_string = 0xbcd;
45+
}
46+
}
47+
48+
You would then use R.drawable.icon to reference the drawable/icon.png file, or Resource.layout.main
49+
to reference the layout/main.xml file, or Resource.strings.first_string to reference the first
50+
string in the dictionary file values/strings.xml.

0 commit comments

Comments
 (0)