mvvm - Can execute question using delegate commands in prism -
this seems dumb question have looked through docs prism , searched internet , can't find example... here deal.
i using delegatecommand in prism, working fine except when assign delegate can execute canexecute method. in view model have event takes bool publishing , can see event firing , bool getting passed view model command in no problem don't understand... how can execute know state has changed? here code example.
from view models ctor
eventaggregator.getevent<navigationenabledevent>().subscribe(onnavigationenabledchange, threadoption.uithread); navigatecommand = new delegatecommand(onnavigate, () => nextbuttonenabled);
now - here onnavigationenablechange event.
private void onnavigationenabledchange(bool navigationstate) { nextbuttonenabled = navigationstate; } enter code here
like - totally missing here - how command know nextbuttonenabled no true?
if point me working example awesome.
ok - thanks!
this why don't use implementation of delegatecommand
in prism. i've hated callback-based approach enabling/disabling commands. it's entirely unnecessary, , far can tell, (and rather doubtful) 'benefit' it's consistent how execution handled. has seemed pointless me because execution , enabling/disabling different: button knows when wants execute command doesn't know when command's status might have changed.
so end writing this:
public class relaycommand : icommand { private bool _isenabled; private action _onexecute; public relaycommand(action executehandler) { _isenabled = true; _onexecute = executehandler; } public bool isenabled { { return _isenabled; } set { _isenabled = value; if (canexecutechanged != null) { canexecutechanged(this, eventargs.empty); } } } public bool canexecute(object parameter) { return _isenabled; } public event eventhandler canexecutechanged; public void execute(object parameter) { _onexecute(); } }
(if necessary modify use weak references execute change event handlers, prism does.)
but answer question: how callback approach meant work? prism's delegatecommand
offers raisecanexecutechanged
method can invoke ask raise event that'll cause command invokers query command's canexecute
. given have tell delegatecommand
time enabled status changes, don't see meaningful benefit of callback-based approach. (sometimes see broadcast model though - arranging change in status anywhere notifies all command invokers! in case, callback useful because means doesn't matter if don't know changed. requerying every single command seems unpleasant me.)
Comments
Post a Comment