Async Method Causing Strange Exception
I have a WPF DataGrid that is bound to a ViewModel and everything is
great. I now realise I need to load a lot more data into the DataGrid than
first anticipated. So I have used async to make the loading of that data
into the Viewmodel container non-UI blocking. So, in my
ResourceDataViewModel : ViewModelBase I have the following method
public async void LoadResourceFilesAsync(string fullFileName = null)
{
if (!String.IsNullOrEmpty(fullFileName))
this.FullFileName = fullFileName;
strategy = manager.InitialiseStrategy(this);
if (strategy == null)
return;
...
// Get data asynchoniously.
List<ResourceViewModel> resourceViewModelList = await
strategy.LoadResourceFilesAsync();
this.Resources = new
TypedListObservableCollection<ResourceViewModel>(resourceViewModelList);
// Setup event handlers.
this.Resources.CollectionChanged += this.OnCollectionChanged;
foreach (ResourceViewModel rvm in resourceViewModelList)
rvm.PropertyChanged += this.OnResourceViewModelPropertyChanged;
// Set the file name for View and ViewModel.
this.FullFileName = strategy.FullFileName;
base.DisplayName = Path.GetFileName(this.FullFileName);
...
}
So, prior to my making the load method async, all was well; the data was
loaded and the DisplayName (property defined in the abstract ViewModelBase
class) which is bound to the TabItem header property caused the file name
to be displayed in the TabItem header correctly. Now I have made the load
asynchronious, the data loads into the DataGrid fine as you would expect
form the code above, however,
base.DisplayName = Path.GetFileName(this.FullFileName);
does not update the base.DisplayName instead it silently (i.e. does not
throw and exeption in my code) causes a System.Exception with message:
Message = "Cannot find the method on the object instance."
Now, I have changed base.DisplayName to **this**.DisplayName and this
stops the exception, but still does not update the TabItem Header, why is
this happening and how do I fix it?
Thanks for your time.
No comments:
Post a Comment