0

I updated Visual Studio and now I am getting a lot of messages like this:

public ICommand OpenWebPageCmd => new Command<string>(async (url) =>
{
   await Browser.OpenAsync(new Uri(url), BrowserLaunchMode.SystemPreferred);
});

Asynchronous method 'Anonymous' should not return void

I saw some other answers that were not specific to this extension. Is there any way I can resolve this when using the Browser option to open a new page?

Alan2
  • 23,493
  • 79
  • 256
  • 450

1 Answers1

0

As the answer here explains that

async await is only compatible with Func<Task> or Func<Task<T>> if you don't have that then you have what is considered an "Async void" which you should not do.

Since Browser.OpenAsync return a Task and not a Func<Task> you have two options

  1. Not await the task:

     public ICommand OpenWebPageCmd => new Command<string>((url) =>
     {
      Browser.OpenAsync(new Uri(url), BrowserLaunchMode.SystemPreferred);
     });
    
  2. Use a better delegate command that supports async functions. Like the AsyncCommand from the Nito.Mvvm.Async NuGet package written by Stephen Cleary.

For more information read the accepted answer here

FreakyAli
  • 13,349
  • 3
  • 23
  • 63