1. Create WCF service. Sample
WCF service can be read from this link
2. Create solution from Xamarin.
3. Add the WCF service as Web reference in the solution.
My WCF service reference name is n192_168_50_233
4. Create one activity and place the controls in the below format,
<TextView> <EditText>
<Button>
<ListView>
Sample UI code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:text="Search"
android:id="@+id/txtView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp" />
<EditText
android:id="@+id/txtPatentType"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#ffe4e9f0"
android:layout_toRightOf="@+id/txtView"
android:layout_margin="10dp" />
<Button
android:id="@+id/btnSearch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btnText"
android:layout_below="@+id/txtView"
android:layout_alignParentRight="true"
android:layout_marginRight="10dp" />
<ListView
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/FeedsCollection"
android:layout_below="@+id/btnSearch"
android:layout_margin="10dp" />
<TextView
android:text=""
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView1"
android:layout_below="@+id/txtView"
android:layout_toLeftOf="@+id/btnSearch"
android:layout_alignParentLeft="true"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp" />
</RelativeLayout>
5. Write the below code in Activity.cs file.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using System.ServiceModel;
using System.Threading;
namespace PatentDetails
{
[Activity (Label = "Patent Feeds", MainLauncher=true, Icon = "@drawable/icon")]
public class GetPatentFeedActivity : Activity
{
private Button search;
private EditText patentType;
private ListView feedsCollection;
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
SetContentView (Resource.Layout.GetPatentFeed);
search = FindViewById<Button> (Resource.Id.btnSearch);
patentType = FindViewById<EditText> (Resource.Id.txtPatentType);
feedsCollection = FindViewById<ListView> (Resource.Id.FeedsCollection);
search.Click += SearchPatentType;
}
public void SearchPatentType(object sender, EventArgs args){
feedsCollection.Adapter = null;
var feedProgress = ProgressDialog.Show (this, "", "Contacting server. Please wait..", true);
n192_168_50_233.Service1 service = new PatentDetails.n192_168_50_233.Service1 ();
new Thread (new ThreadStart (delegate {
RunOnUiThread(()=>{
feedProgress.Show();
IAsyncResult asyncResult=service.BeginGetPatentList (patentType.Text, patentFeeds => { },null);
var patent = service.EndGetPatentList (asyncResult);
Toast.MakeText (this,"Feeds count " + patent.Length,ToastLength.Long).Show();
//Load Feeds in CustomFeedsLayout
feedsCollection.Adapter =new FeedsAdapter(this,patent);} );
RunOnUiThread(()=>feedProgress.Hide());
})).Start ();
}
}
}
WCF service can be read from this link
2. Create solution from Xamarin.
3. Add the WCF service as Web reference in the solution.
My WCF service reference name is n192_168_50_233
4. Create one activity and place the controls in the below format,
<TextView> <EditText>
<Button>
<ListView>
Sample UI code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:text="Search"
android:id="@+id/txtView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp" />
<EditText
android:id="@+id/txtPatentType"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#ffe4e9f0"
android:layout_toRightOf="@+id/txtView"
android:layout_margin="10dp" />
<Button
android:id="@+id/btnSearch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btnText"
android:layout_below="@+id/txtView"
android:layout_alignParentRight="true"
android:layout_marginRight="10dp" />
<ListView
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/FeedsCollection"
android:layout_below="@+id/btnSearch"
android:layout_margin="10dp" />
<TextView
android:text=""
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView1"
android:layout_below="@+id/txtView"
android:layout_toLeftOf="@+id/btnSearch"
android:layout_alignParentLeft="true"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp" />
</RelativeLayout>
5. Write the below code in Activity.cs file.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using System.ServiceModel;
using System.Threading;
namespace PatentDetails
{
[Activity (Label = "Patent Feeds", MainLauncher=true, Icon = "@drawable/icon")]
public class GetPatentFeedActivity : Activity
{
private Button search;
private EditText patentType;
private ListView feedsCollection;
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
SetContentView (Resource.Layout.GetPatentFeed);
search = FindViewById<Button> (Resource.Id.btnSearch);
patentType = FindViewById<EditText> (Resource.Id.txtPatentType);
feedsCollection = FindViewById<ListView> (Resource.Id.FeedsCollection);
search.Click += SearchPatentType;
}
public void SearchPatentType(object sender, EventArgs args){
feedsCollection.Adapter = null;
var feedProgress = ProgressDialog.Show (this, "", "Contacting server. Please wait..", true);
n192_168_50_233.Service1 service = new PatentDetails.n192_168_50_233.Service1 ();
new Thread (new ThreadStart (delegate {
RunOnUiThread(()=>{
feedProgress.Show();
IAsyncResult asyncResult=service.BeginGetPatentList (patentType.Text, patentFeeds => { },null);
var patent = service.EndGetPatentList (asyncResult);
Toast.MakeText (this,"Feeds count " + patent.Length,ToastLength.Long).Show();
//Load Feeds in CustomFeedsLayout
feedsCollection.Adapter =new FeedsAdapter(this,patent);} );
RunOnUiThread(()=>feedProgress.Hide());
})).Start ();
}
}
}
6. Write the below code in FeedsAdapter.cs
using System;
using Android.Widget;
using PatentDetails.n192_168_50_233;
using Android.App;
using Android.Views;
namespace PatentDetails
{
public class FeedsAdapter : BaseAdapter<PatentModel>
{
PatentModel[] patentList;
Activity activity;
public FeedsAdapter(Activity _activity, PatentModel[] _patentList):base(){
activity = _activity;
patentList = _patentList;
}
#region implemented abstract members of BaseAdapter
public override PatentModel this [int index] {
get {
return patentList [index];
}
}
public override long GetItemId (int position)
{
return position;
}
public override Android.Views.View GetView (int position, Android.Views.View convertView, Android.Views.ViewGroup parent)
{
var patentItem = patentList [position];
View view = convertView;
if (view == null)
view = activity.LayoutInflater.Inflate (Resource.Layout.CustomFeedsLayout, null);
view.FindViewById<TextView> (Resource.Id.textViewTitle).Text = patentItem.Title;
view.FindViewById<TextView> (Resource.Id.textViewLink).Text = patentItem.Link;
view.FindViewById<TextView> (Resource.Id.textViewDescription).Text = patentItem.Description;
view.FindViewById<TextView> (Resource.Id.textViewPublishedDate).Text = patentItem.PublishedDate;
return view;
}
public override int Count {
get {
return patentList.Length;
}
}
#endregion
}
}
7. Create One Layout file in the name of CustomFeedsLayout.XAML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:text="Text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textViewTitle"
android:textColor="#ffec9f10" />
<TextView
android:text="Text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textViewLink"
android:textColor="#ff577ae8" />
<TextView
android:text="Text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textViewDescription"
android:textColor="#ffe20ddf" />
<TextView
android:text="Text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textViewPublishedDate"
android:textColor="#ff38d6d6" />
</LinearLayout>
8. Xamarin with WCF service consume project is ready..
9. Happy coding.
Nice Post.
ReplyDelete