I'm creating an application (an Office Add-in for Outlook)
The issue I have is updating my screen. I know I need to use invoke the Dispatcher but, it's always null in my ViewModel
private ObservableCollection<string> _updates;
public ObservableCollection<string> Updates
{
get { return this._updates; }
set
{
this._updates = value;
OnPropertyChanged("Updates");
}
}
BackgroundWorker bw = new BackgroundWorker();
bw.DoWork += ((s, e) =>
{
//logic
UpdateProgress("Finished");
});
bw.RunWorkerAsync();
private void UpdateProgress(string s)
{
//Application.Current.Dispatcher.Invoke(() =>
// {
App.Current.Dispatcher.Invoke(() =>
{
this.Updates.Add(s);
//});
});
}
As you can see, I've tried 2 approaches, but Current
is always null.
Oddly, if I use the same code in the code behind of my MainWindow then the following works fine
private void UpdateProgress(string s)
{
Dispatcher.Invoke(() =>
{
this.Update = s;
});
}
I've read up, the reason is because the MainWindow code behind inherits from Window.
My question is, do I have to create a new Dispatcher object or is there something I'm missing. All I'm trying to do is update my GUI whilst the thread is running.