Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Manually executing a command in ReactiveUI 7.2

Tags:

c#

wpf

reactiveui

I'm trying to execute a command so that a function I have runs asynchronously in from within the constructor of my view. I understand that running something asynchronously in a constructor isn't necessarily good practice, but I need to load a datagrid when the view opens which takes anywhere from 3-5 seconds, so I thought I could load a progress bar with an IsLoading property while my command to fetch the data runs on a separate thread.

Here is my command / property declaration to do the above:

public ReactiveCommand EnterCmd { get; private set; }
ObservableAsPropertyHelper<bool> _isLoading;
public bool IsLoading => _isLoading.Value;

And here are my instantiations in the constructor:

EnterCmd = ReactiveCommand.CreateFromTask(LoadGridData);
EnterCmd.IsExecuting.ToProperty(this, x => x.IsLoading, out _isLoading);

I'd like to run start my command by doing something like EnterCmd.Execute(). It's worth noting that I did find a similar question here, but Execute() doesn't seem to be a valid method on a ReactiveCommand anymore. Any ideas would be appreciated.

like image 669
jewnbug97 Avatar asked Nov 28 '22 13:11

jewnbug97


1 Answers

The Exexute() method was changed in version 7. It now returns an observable, so you need to subscribe to it for it to work:

EnterCmd.Execute().Subscribe();
like image 196
Jon G Stødle Avatar answered Dec 09 '22 09:12

Jon G Stødle