リレーコマンドハンドラーを定義するラムダステートメントでデータサービスのインジェクトされたインスタンスを使用すると、ハンドラーが呼び出されることはありません(ボタンに関連付けられています)。ラムダ内でデータサービスのインスタンスを宣言すると、正常に機能します。何か案は?
編集:クラス変数_dataServiceを作成し、ビューモデルコンストラクターで初期化しました。リレーコマンドハンドラー内でクラス変数を使用すると、すべてが機能します。
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
}