c# - Xamarin.Forms: ListView are not being Displayed on Xamarin.Droid -
i'm creating xamarin.forms portable application. have database in visual studio , want display data inside xamarin listview. whenever that, data not being displayed on xamarin.droid leaving blank space. tried in uwp , worked. how in xamarin.droid?
(screenshot of xamarin.droid)
notice listview still occupy space if records not being displayed. think reason behind this? check in web api if data being retrieved , does.
meaning, real problem occurs in displaying records on listview. hope can me.
here codes i've tried.
clientlist.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="xamarinformsdemo.views.clientlistpage" xmlns:viewmodels="clr-namespace:xamarinformsdemo.viewmodels;assembly=xamarinformsdemo" xmlns:controls="clr-namespace:imagecircle.forms.plugin.abstractions;assembly=imagecircle.forms.plugin.abstractions" backgroundimage="bg3.jpg" title="client list"> <contentpage.bindingcontext> <viewmodels:customervm/> </contentpage.bindingcontext> <stacklayout orientation="vertical"> <searchbar placeholder="search" text="{binding keyword}" searchcommand="{binding searchcommand}" x:name="txtsearch" /> <listview itemssource="{binding customerlist}" hasunevenrows="true"> <listview.itemtemplate> <datatemplate> <viewcell> <grid padding="10" rowspacing="10" columnspacing="5"> <grid.rowdefinitions> <rowdefinition height="auto"/> <rowdefinition height="*"/> </grid.rowdefinitions> <grid.columndefinitions> <columndefinition width="auto"/> <columndefinition width="*"/> </grid.columndefinitions> <controls:circleimage source="icon.png" heightrequest="66" horizontaloptions="centerandexpand" aspect="aspectfill" widthrequest="66" grid.rowspan="2" /> <label grid.column="1" text="{binding customer_name}" textcolor="#24e97d" fontsize="24"/> <label grid.column="1" grid.row="1" text="{binding customer_code}" textcolor="white" fontsize="18" opacity="0.6"/> <label grid.column="1" grid.row="2" text="{binding customer_contact}" textcolor="white" fontsize="18" opacity="0.6"/> </grid> </viewcell> </datatemplate> </listview.itemtemplate> </listview> <stacklayout orientation="vertical" padding="30,10,30,10" heightrequest="20" backgroundcolor="#24e97d" verticaloptions="center" opacity="0.5"> <label text="© copyright 2016 smesoft.com.ph rights reserved " horizontaltextalignment="center" verticaloptions="center" horizontaloptions="center" /> </stacklayout> </stacklayout> </contentpage>
clientlistviewmodel.cs
using system; using system.collections.generic; using system.collections.objectmodel; using system.componentmodel; using system.diagnostics; using system.linq; using system.runtime.compilerservices; using system.text; using system.threading.tasks; using system.windows.input; using xamarin.forms; using xamarinformsdemo.models; using xamarinformsdemo.services; namespace xamarinformsdemo.viewmodels { public class customervm : inotifypropertychanged { private list<customer> _customerlist; // keep customers private list<customer> _searchedcustomerlist; // keep copy searching private customer _selectedcustomer = new customer(); private string _keyword = ""; public string keyword { { return _keyword; } set { this._keyword = value; // while keyword changed filter employees //filter(); } } private void filter() { if (string.isnullorwhitespace(_keyword)) { customerlist = _searchedcustomerlist; } else { // var lowerkeyword = _keyword.tolower(); customerlist = _searchedcustomerlist.where(r => r.customer_name.tolower().contains(_keyword.tolower())).tolist(); // employeeslist = _searchedemployeeslist.where(r => r.employee_name.contains(_keyword)).tolist(); } } public list<customer> customerlist { { return _customerlist; } set { _customerlist = value; onpropertychanged(); } } public icommand searchcommand { { return new command((sender) => { //var searchbar = (searchbar)sender; //this.keyword = searchbar.text; filter(); }); } } public customervm() { initializedataasync(); } private async task initializedataasync() { var customerservices = new customerservices(); _searchedcustomerlist = await customerservices.getcustomerasync(); customerlist = await customerservices.getcustomerasync(); } public event propertychangedeventhandler propertychanged; protected virtual void onpropertychanged([callermembername] string propertyname = null) { var handler = propertychanged; if (handler != null) handler(this, new propertychangedeventargs(propertyname)); } } }
customerservice.cs
using plugin.restclient; using system; using system.collections.generic; using system.collections.objectmodel; using system.linq; using system.text; using system.threading.tasks; using xamarinformsdemo.models; namespace xamarinformsdemo.services { public class customerservices { public async task<list<customer>> getcustomerasync() { restclient_customer<customer> restclient = new restclient_customer<customer>(); var customerlist = await restclient.getcustomerasync();//yung getasync ay pantawag restclient return customerlist; } } }
restclient.cs
public class restclient_customer <t> { private const string webserviceurl = "http://localhost:50857/api/customer/"; public async task<list<t>> getcustomerasync() { var httpclient = new httpclient(); var json = await httpclient.getstringasync(webserviceurl); var taskmodels = jsonconvert.deserializeobject<list<t>>(json); return taskmodels; } }
in viewmodel change
public list<customer> customerlist
to
public observablecollection<customer> customerlist
and in xaml, change
<listview itemssource="{binding customerlist}"
to
<listview itemssource="{binding customerlist, mode=twoway}"
Comments
Post a Comment