mvvm - MvvmCross binding property of custom type to custom view -
i tried bind property of custom type in view model custom view, in binder method "setvalue(object value)" value null. why happens? impossible bind property of custom type custom view in mvvmcross?
my viewmodel:
public class recipesfiltersvm : mvxviewmodel { public searchfield dishfield { get; private set; } public searchfield cuisinefield { get; private set; } public searchfield ingredientfield { get; private set; } private readonly ifiltersservice _filtersservice; public recipesfiltersvm(ifiltersservice filtersservice) { _filtersservice = filtersservice; updatesearchfields (); } private async void updatesearchfields () { var allfilters = await _filtersservice.loadallfilters (); dishfield = new searchfield ( allfilters .where(f => f.type == filtertype.dish) .tolist() ); cuisinefield = new searchfield ( allfilters .where(f => f.type == filtertype.cuisine) .tolist() ); ingredientfield = new searchfield ( allfilters .where(f => f.type == filtertype.ingredient) .tolist() ); } }
my searchfield:
public class searchfield : mvxviewmodel { private string _searchresult; public string searchresult { { return _searchresult; } set { _searchresult = value; raisepropertychanged (() => searchresult); updatefoundfilters (); } } private observablecollection<filter> _foundfilters; public observablecollection<filter> foundfilters { { return _foundfilters; } set { _foundfilters = value; raisepropertychanged (() => foundfilters); } } }
in customview:
public class searchfieldview : uiview { public uitextfield searchresult { { return _searchresult; } } private uitextfield _searchresult; public uitableview foundfilters { { return _foundfilters; } } private uitableview _foundfilters; }
in binder:
public class searchfieldviewwithsearchfieldbinder : mvxtargetbinding { protected searchfieldview searchfieldview { { return (searchfieldview)target; } } public searchfieldviewwithsearchfieldbinder (searchfieldview target) : base (target) { } public override void setvalue (object value) { //value null! } public override type targettype { { return typeof(searchfield); } } public override mvxbindingmode defaultmode { { return mvxbindingmode.twoway; } } }
setup:
protected override void filltargetfactories (cirrious.mvvmcross.binding.bindings.target.construction.imvxtargetbindingfactoryregistry registry) { registry.registercustombindingfactory<searchfieldview> ( "searchfield", indicators => new searchfieldviewwithsearchfieldbinder(indicators) ); base.filltargetfactories (registry); }
in viewcontroller:
var set = this.createbindingset<recipesfiltersdialog, recipesfiltersvm>(); set.bind (_dish).for("searchfield").to (vm => vm.dishfield); set.bind (_cuisine).for("searchfield").to (vm => vm.cuisinefield); set.bind (_ingredient).for("searchfield").to (vm => vm.ingredientfield); set.apply ();
upd
solved 2 ways update viewmodel code. first changed custom property declaration like:
private searchfield _dishfield; public searchfield dishfield { { return _dishfield; } set { _dishfield = value; raisepropertychanged (() => dishfield); } }
second initialize properties in viewmodel constructor before updatesearchfields () execution:
public recipesfiltersvm(ifiltersservice filtersservice) { _filtersservice = filtersservice; dishfield = new searchfield (new list<filter> ()); cuisinefield = new searchfield (new list<filter> ()); ingredientfield = new searchfield (new list<filter> ()); updatesearchfields (); }
you need create own custom bindings custom view. instance if have followed normal mvvmcross practice of using public c# properties , c# events define items in custom view should able this:
public class searchfieldview : uiview, imvxbindable { public imvxbindingcontext bindingcontext { get; set; } public searchfieldview() { this.createbindingcontext(); } public uitextfield searchresult { { return _searchresult; } } private uitextfield _searchresult; public uitableview foundfilters { { return _foundfilters; } } private uitableview _foundfilters; [mvxsettonullafterbinding] public object datacontext { { return bindingcontext.datacontext; } set { bindingcontext.datacontext = value; } } protected override void dispose(bool disposing) { if (disposing) { bindingcontext.clearallbindings(); } base.dispose(disposing); } }
and should "work." inspiration checkout things mvxview or mvxtableviewcell.
alternatively may able own mvxpropertyinfotargetbinding how mvvmcross handling uisegmentedcontrolbindings implementing mvxuisegmentedcontrolselectedsegmenttargetbinding.
Comments
Post a Comment