Monday, 15 March 2010

mvvm - Does anyone know a good example of ReactiveCommand for ReactiveUI? -



mvvm - Does anyone know a good example of ReactiveCommand for ReactiveUI? -

i'm inexperienced, @ mvvm, trying utilize reactiveui, , i'm not understanding examples i'm finding demonstrate reactivecommand. have used icommand / delegatecommand 1 time before, different, , i'm not getting it.

what i'm trying simple. click button in view, , have execute method in view model. examples i'm finding involve iobservable<>, , don't that, don't explanations geared total noob am.

basically, i'm trying utilize learning experience, , i'd ideally bind button's command property in xaml command (however works, don't know), causes method execute. no collections, i'd passing single int variable.

thanks help. appreciate it.

edit - below appears code using paul betts' suggestions:

public reactivecommand addtodailyused { get; protected set; } public mainpagevm() { initialize(); addtodailyused = new reactivecommand(); addtodailyused.subscribe(addtotodayusedaction => this.addtodailyusedexecuted()); } private object addtodailyusedexecuted() { messagebox.show("addtodailyusedaction"); homecoming null; } private void addtodailyusedaction(object obj) { messagebox.show("addtodailyusedaction"); } <button content="{binding strings.add, source={staticresource localstrings}}" command="{binding addtotodayused}" margin="-5,-10, -10,-10" grid.row="3" grid.column="2" />

obviously i'm missing something. inserted break points @ addtodailyusedexecuted , addtodailyusedaction methods, , never reached.

edit constructor code behind view:

mainpagevm mainpagevm = new mainpagevm(); public mainpage() { initializecomponent(); speech.initialize(); datacontext = mainpagevm; applicationbar = new applicationbar(); taskregistration.registerscheduledtask(); this.loaded += new routedeventhandler(mainpage_loaded); //shows rate reminder message, according settings of ratereminder. (app.current app).ratereminder.notify(); }

so, reactivecommand iobservable<object> - in case, can conceptualize iobservable event - event fires when command invoked (i.e. when button pressed). so, in constructor, might write:

mycommand = new reactivecommand(); mycommand.subscribe(param => this.mycommandhasexecuted());

however, what's neat iobservable isn't true regular events, can utilize linq on them:

// now, mycommandhasexecuted gets run when username isn't null mycommand.where(param => this.username != null) .subscribe(param => this.mycommandhasexecuted());

update: xaml binds addtotodayused viewmodel command called addtodailyused. it?

mvvm windows-phone-8 windows-phone reactiveui

No comments:

Post a Comment