public MainPageViewModel(ILogger<MainPageViewModel> logger, WorkerService workerService)
{
_logger = logger;
_workerService = workerService;
StartTheServiceCommand = new Command(
execute: () =>
{
_logger.LogInformation("Starting the service at: {time}", DateTimeOffset.Now);
Task.Run(async () => await _workerService.StartAsync(new()));
RefreshCanExecutes();
},
canExecute: () => !_workerService.IsRunning);
StopTheServiceCommand = new Command(
execute: () =>
{
_logger.LogInformation("Stopping the service at: {time}", DateTimeOffset.Now);
Task.Run(async () => await _workerService.StopAsync(new()));
RefreshCanExecutes();
},
canExecute: () => _workerService.IsRunning);
}
public ICommand StartTheServiceCommand { get; }
public ICommand StopTheServiceCommand { get; }
private void RefreshCanExecutes()
{
(StartTheServiceCommand as Command)!.ChangeCanExecute();
(StopTheServiceCommand as Command)!.ChangeCanExecute();
}
public MainPageViewModel(ILogger<MainPageViewModel> logger, WorkerService workerService)
{
_logger = logger;
_workerService = workerService;
StartTheServiceCommand = new Command(
execute: () =>
{
_logger.LogInformation("Starting the service at: {time}", DateTimeOffset.Now);
Task.Run(async () => await _workerService.StartAsync(new()));
RefreshCanExecutes();
},
canExecute: () => !_workerService.IsRunning);
StopTheServiceCommand = new Command(
execute: () =>
{
_logger.LogInformation("Stopping the service at: {time}", DateTimeOffset.Now);
Task.Run(async () => await _workerService.StopAsync(new()));
RefreshCanExecutes();
},
canExecute: () => _workerService.IsRunning);
}
public ICommand StartTheServiceCommand { get; }
public ICommand StopTheServiceCommand { get; }
private void RefreshCanExecutes()
{
(StartTheServiceCommand as Command)!.ChangeCanExecute();
(StopTheServiceCommand as Command)!.ChangeCanExecute();
}