Search code examples
c#xamarin.androidmvvmcross

MvxAndroidApplication could not be found


I've successfully followed the "getting started" tutorial for MvvmCross. However, when I tried to create a new project on another computer, following that tutorial as a guide, I could not get it to compile. The "Core" project (the netstandard library) is fine; but when I create a new Xamarin Android project and I define MainApplication (which should inherit from MvxAndroidApplication<MvxAndroidSetup<App>, App>), the two Mvx names cannot be found.

Of course, I've followed very carefully all the instructions of the official tutorial, which means that I've installed the most recent (6.0.1) version of MvvmCross with NuGet. I have cleaned it and rebuilt it. I even explored the MvvmCross assembly that gets included, but it completely lacks the MvvmCross.Platforms namespace that should contain the two classes that I mentioned above.

It seems obvious to me that something is missing or misconfigured in the second computer. I have no idea what, though. One thing that caught my eye is that if I try to add a reference to Mono.Android it is listed as version 0.0.0.0.

How could I fix the issue? What should I check that I may have missed?


Solution

  • I found the solution myself. It involves one undocumented requirement and one weird behaviour of Visual Studio (it certainly has a good reason, but I don't know what it is), so I'm leaving this here for reference.

    To use MvvmCross in a Xamarin Android project, you must target (Mono)Android 8.1. I'm not sure whether that translates to "the most recent Android version" or if 8.1 has something special. Anyway, the NuGet package for MvvmCross includes the relevant DLL for the monoandroid81 target: if your project is compiled for something else (even Mono Android 8.0), then NuGet will add the reference for netstandard20, and that DLL of course does not include anything in the Platforms namespace.

    Once you have changed the build destination in the project settings, you must at least unload and reload the project. Or you can close the solution, but the project is really all that's needed. This step is necessary to get the correct target platform in Visual Studio. To cross-check it, open the Add reference window, and browse the "Framework" listing: at the top of the assembly list you'll find a label showing what is the current target platform. That label must show "MonoAndroid 8.1"; if it doesn't, check the project settings and then reload the project.