साथ ऊपर कोड की जगह मैं अपने आदेश के CanExecute
और Execute
तरीकों कॉल करने के लिए तीन अलग-अलग समाधान पाया है कि मैं पहले ReactiveUI 6.5 में किए जा सकेंगे:
विकल्प 1
यह संस्करण 6.5 में कॉल के बराबर है, लेकिन हमें कमांड को आईसीओएमएंड में स्पष्ट रूप से रूपांतरित करने की आवश्यकता है:
if (((ICommand) command).CanExecute(null))
command.Execute().Subscribe();
विकल्प 2
if(command.CanExecute.FirstAsync().Wait())
command.Execute().Subscribe()
या async संस्करण:
if(await command.CanExecute.FirstAsync())
await command.Execute()
विकल्प 3
एक अन्य विकल्प InvokeCommand
विस्तार विधि के बारे में हमें करना है।
Observable.Start(() => {}).InvokeCommand(ViewModel, vm => vm.MyCommand);
यह आदेश के executability सम्मान करता है, documentation में उल्लेख किया है की तरह।
आदेश में बनाने के लिए यह और अधिक आरामदायक मैं एक ExecuteIfPossible
और एक GetCanExecute
विधि प्रदान करने के लिए एक छोटा सा विस्तार विधि लिखा है:
public static class ReactiveUiExtensions
{
public static IObservable<bool> ExecuteIfPossible<TParam, TResult>(this ReactiveCommand<TParam, TResult> cmd) =>
cmd.CanExecute.FirstAsync().Where(can => can).Do(async _ => await cmd.Execute());
public static bool GetCanExecute<TParam, TResult>(this ReactiveCommand<TParam, TResult> cmd) =>
cmd.CanExecute.FirstAsync().Wait();
}
आप इस प्रकार इस विस्तार विधि का उपयोग कर सकते हैं:
command.ExecuteIfPossible().Subscribe();
नोट: आपको Subscribe()
अंत में कॉल की आवश्यकता है, जैसे कि आपको Execute()
पर कॉल के लिए इसकी आवश्यकता है, अन्य बुद्धिमान कुछ भी नहीं होगा।
या आप async उपयोग करना चाहते हैं और इंतजार करता है, तो:
await command.ExecuteIfPossible();
आप अगर एक कमांड निष्पादित किया जा सकता जाँच करना चाहते हैं, तो बस फोन CanExecute के लिए काम arounds पता लगाना के लिए
command.GetCanExecute()
धन्यवाद (शून्य) 6.5 से उन्नयन करते समय। –
मैं 'InvokeCommand' तरीका पसंद करूंगा। – Felix