Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't I use await keyword in my Windows Phone 7.1 MvvmCross project while using the Microsoft.Bcl - cannot await 'System.Threading.Tasks.Task?

I am unable to use the await keyword in my MvvmCross Windows Phone 7.1 project while using the Microsoft.Bcl "Microsoft BCL Portability Pack".

I have posted the code for the sample project that I describe below on GitHub and the await error occurs on this line.

When I try to build the third "Core" Windows Portable Class Library (PCL) that contains the FirstViewModel, I get the errors of:

Type System.Threading.Tasks.Task<string> not awaitable under VS red squiggles, and
Cannot await 'System.Threading.Tasks.Task<string>' when building.

There are four projects in the solution:

  1. A Windows Portable Class Library (PCL) with an interface in it that references that "Task" object provided by the BCL.

  2. A Windows Phone Class Library targeting OS 7.1 that implements the interface above and also uses "Task" from the BCL.

  3. Another Windows Portable Class Library (PCL) that provides the "Core" functionality of the app, including the ViewModel.

  4. The Windows Phone 7.1/"7.5" application that tries to reference and consume the DLL's above.

The first two projects containing the interface and the implementation of the interface compile without any errors. But the 3rd "Core" PCL fails to build with the errors shown above.

More Details If You Want Them:

  • I tried this App.config "fix" as described here in the "known BCL issues": http://blogs.msdn.com/b/bclteam/p/asynctargetingpackkb.aspx .But that did not resolve the error when I added App.config to just the "Core" project, nor when I tried to add the same App.config to every project. It is possible that I am not using the right version in the element or I just did the App.config stuff incorrectly.

  • Every project in the solution that is set to target a PCL are set to "Profile104" in Visual Studio.

Those DLL's are located in:

C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETPortable\v4.0\Profile\Profile104\
  • This PCL profile is shown as targeting these platforms in the Visual Studio project properties: .NET for Windows Store apps, .NET 4.5, Silverlight 4+, and Windows 7.5+

  • Every project in the solution also has reference to the Microsoft.Bcl "Microsoft BCL Portability Pack" via NuGet.

  • All four projects are using MvvmCross (Mvx).

  • None of the projects in this sample are trying to target anything related to the Xamarin Droid/iOS stack yet. It is all Windows and is still failing on the await keyword.

  • The PCL with the interface in it and the Windows Phone Class Library both use the Mvx CrossCore NuGet Package and are setup to be consumed as "Mvx Plugins".

  • The "Core" PCL is using the full MvvmCross NuGet package and contains the FirstViewModel where the await error is occurring.

  • The Windows Phone App project references the full MvvmCross as well, with the Windows Phone-specific Mvx files pulled in to it.

  • as mentioned, see the failing sample code on GitHub.

like image 700
Kerry Avatar asked Dec 16 '22 07:12

Kerry


1 Answers

You need to add the Microsoft.Bcl.Async NuGet package.

These packages were previously unavailable on non-Microsoft platforms, but as part of the Xamarin collaboration Microsoft relicensed them to be usable under all platforms.

like image 122
Stephen Cleary Avatar answered Dec 18 '22 12:12

Stephen Cleary