android - Xamarin TimePicker propertychanged event fired on load -
i developing xamarin app android using xamarin forms create layout. have come across issue time picker firing on load of view cell.
what doing creating list view , setting item template custom view cell template. in view cell template creating timepicker , binding propertychanged event it. setting timepicker.time property information retrieved database. seems happen @ point propertychanged event fired each item displayed in list view. leads multiple database calls not needed.
is there way stop propertychanged event being called until property has been changed?
my code below.
public class mycell : viewcell { private readonly timepicker _mytimepicker; public mycell() { _mytimepicker = new timepicker() { horizontaloptions = layoutoptions.endandexpand }; _mytimepicker.setbinding(timepicker.timeproperty, "starttime"); _mytimepicker.propertychanged += mytimepicker_propertychanged; var viewlayout = new stacklayout() { horizontaloptions = layoutoptions.startandexpand, orientation = stackorientation.horizontal, padding = new thickness(20), children = { _mytimepicker } }; view = viewlayout; } private void mytimepicker_propertychanged(object sender, system.componentmodel.propertychangedeventargs e) { if (e.propertyname == timepicker.timeproperty.propertyname && _mytimepicker.time != timespan.minvalue) { var dataaccess = new dataaccesslayer(); dataaccess.update(mytimepicker.time); } } }
i'm not sure why propertychanged event fired multiple times , how stop firing until pick time. appreciated.
below code form display list view. xaml defined in code. 'mycell' uses values returned '_dataaccess.gettimes()'.
public class timedetails : contentpage { private listview _listview; private readonly dataaccesslayer _dataaccess = new dataaccesslayer(); protected override void onappearing() { _listview = new listview { rowheight = 80, separatorcolor = color.blue, separatorvisibility = separatorvisibility.default }; _listview.itemssource = _dataaccess.gettimes(); _listview.itemtemplate = new datatemplate(typeof(mycell)); _listview.itemselected += listview_itemselected; content = new stacklayout { verticaloptions = layoutoptions.fillandexpand, children = { _listview } }; } }
the return type of _dataacess.gettimes() list of tasktime. tasktime model shown below.
public class tasktime { public int id { get; set; } public string task { get; set; } public timespan starttime { get; set; } }
class mycell : viewcell { private readonly timepicker _mytimepicker; public mycell() { _mytimepicker = new timepicker() { horizontaloptions = layoutoptions.endandexpand }; _mytimepicker.setbinding(timepicker.timeproperty, "starttime"); _mytimepicker.propertychanged += mytimepicker_propertychanged; _mytimepicker.focused += _mytimepicker_focused; var viewlayout = new stacklayout() { horizontaloptions = layoutoptions.startandexpand, orientation = stackorientation.horizontal, padding = new thickness(20), children = { _mytimepicker } }; view = viewlayout; } bool mytimepickerselected; private void _mytimepicker_focused(object sender, focuseventargs e) { mytimepickerselected = true; } private void mytimepicker_propertychanged(object sender, system.componentmodel.propertychangedeventargs e) { if (e.propertyname == timepicker.timeproperty.propertyname && mytimepickerselected) { //var dataaccess = new dataaccesslayer(); //dataaccess.update(mytimepicker.time); } } }
Comments
Post a Comment