Search code examples
c#.netwpfmvvmdispatcher

Dispatcher' does not contain a definition for 'InvokeAsync' and no extension method 'InvokeAsync'


Please I'm having error and this is my code.

private void ComboBoxSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            _comboBox.Dispatcher.InvokeAsync(() => ContentChanged?.Invoke(sender, EventArgs.Empty));
        }

its saying Dispatcher' does not contain a definition for 'InvokeAsync' and no extension method 'InvokeAsync' accepting a first argument of type 'Dispatcher' could be found (are you missing a using directive or an assembly reference? wpf I'm lost please, i need help on these. Thanks.


Solution

  • Dispatcher.InvokeAsync is definitely an existing method as of .NET 4.5. You'll see that error if you try to compile for .NET 4.0 or earlier.

    It has the same effect as if you had called Dispatcher.BeginInvoke. The difference is BeginInvoke accepts a delegate(requiring a cast from a lambda), whereas InvokeAsync doesn't, because it accepts an Action. This was done to refactor the API, but in a manner that didn't break code still using BeginInvoke. See this thread for more details.

    Before .NET 4.5:

    _comboBox.Dispatcher.BeginInvoke((Action)(() => {
        ContentChanged?.Invoke(sender, EventArgs.Empty);
    }));
    

    Since .NET 4.5:

    _comboBox.Dispatcher.InvokeAsync(() => {
        ContentChanged?.Invoke(sender, EventArgs.Empty);
    });