0

When I use an injected instance of a data service in the lambda statement that defines a relay command handler, the handler is never invoked (it is associated with a button). When I declare an instance of the data service within the lambda, it works fine. Any ideas?

Edited: Created the class variable _dataService and initialized it in the view model constructor. Use the class variable within the relay command handler and all works.

    private IDataService _dataService;
    public MainViewModel(IDataService dataService)
    {
        _dataService = dataService;
        Batches = new ObservableCollection<Batch>();

        #region RefreshCommand
        RefreshCommand = new RelayCommand(
            () =>
            {
                var t1 = Task<ObservableCollection<Batch>>.Factory.StartNew(() =>
                {
                    // WHEN I UNCOMMENT AND COMMENT OUT BELOW, WORKS FINE.
                    //DataService test = new DataService();
                    //ObservableCollection<Batch> batches = test.GetBatchesToProcess();

                    //
                    // THIS NOW WORKS.
                    return _dataService.GetBatchesToProcess();
                });
                try
                {
                    t1.Wait();
                }
                catch (AggregateException ae)
                {
                    foreach (var e in ae.InnerExceptions)
                    {
                        if (e is SqlException)
                        {
                            MessageBox.Show("Sql Exception: " + e.Message);
                        }
                        else
                        {
                            MessageBox.Show("Unexpected error: " + e.Message);
                        }
                    }
                    return;
                }
                Batches = t1.Result;
            }
        );
        #endregion  
    }
Seth Abady
  • 25
  • 1
  • 5

1 Answers1

0

Using the dataService parameter to the MainViewModel constructor did not work within the relay command handler. Using a private class variable (_dataService) that is initialized within the constructor solved the dilemma.

Seth Abady
  • 25
  • 1
  • 5