c# - How to execute a command only once using ReactiveUI in Xamarin.Forms? -


using rxui xamarin.forms, how create command meant executed once automatically (when page appears) te user can request execution later on (like pull refresh kind of event)?

i've hooked command appearing event using fromeventpattern when navigate page gets executed again undesired behavior.

this scenario: need list populated automatically when user opens page contains it. user can select element , view details in separate page (using navigationpage), when user returns list page gets repopulated should not happen. user should able request new data vía button or pull refresh, though.

thank you.

here's how i've handled scenario. you'll need behavior similar one, invoke command when specific, known value set.

// invoke command when property matches known value can't happen through normal execution this.whenanyvalue(vm => vm.someproperty)                 .where(sp => sp == null)                 .throttle(timespan.fromseconds(.25), taskpoolscheduler.default)                 .do(_ => debug.writeline($"refresh list"))                 .invokecommand(getlist)                 .disposewith(subscriptiondisposables); 

at end of constructor, set someproperty match known value

this.someproperty = null; // or value makes sense 

in case, don't need fire command manually in onappearing - happens when viewmodel constructed first time , not executed again until viewmodel disposed , recreated. seems little hack-ey me, i'm hoping wiser, more seasoned rxui wizards chime in, gets job done.

if prefer leave onappearing call in place, potentially handle setting canexecute property of reactivecommand , using entirely different reactivecommand pulltorefresh action (even though both commands behave same otherwise). in scenario, you'd want make canexecute false after list populated initially, when user return page initial population isn't triggered.

var isinitialized = this.whenanyvalue(vm => vm.isinit).select( _ => _ == false).distinctuntilchanged();  initlist = reactivecommand.createfromtask( _ =>  {      // list  }, isinitialized);  refreshlist = reactivecommand.createfromtask( _ => {     // same initlist, different/no canexecute parameters });  initlist.observeon(rxapp.mainthreadscheduler).subscribe(result => {     this.isinit = false }).disposewith(subscriptiondisposables); 

the disadvantage here is, obviously, have logic duplicated


Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -