1

I have a .NET MAUI project that shows these 3 compile errors (that shouldn't occur), but it compiles and runs successfully anyway:

enter image description here

Here is the source of my MainPage.xaml.cs:

using Android.Media;

namespace MovieDoo;

public partial class MainPage : ContentPage
{
   
   
   MediaPlayer _player;
   string _userID = "777";

   public MainPage()
    {
        InitializeComponent();
      _player = new MediaPlayer();
   }

    private void OnAudioClicked(object sender, EventArgs e) {
      _player.SetDataSource("https://samplelib.com/lib/preview/mp3/sample-6s.mp3");
      _player.Prepare();
      _player.Start();
   }

   private void getQuestion() {
      string encryptedUserID = Utilities.Encrypt(_userID);
   }

   private void OnSubmitClicked(object sender, EventArgs e) {
      string movieTitle = txtMovieTitle.Text;

      if(!string.IsNullOrEmpty(movieTitle)) {
         if(txtMovieTitle.Text.Trim().ToLower().Contains("casablanca")) {
            toggleCorrectIncorrect(true);
         } else {
            toggleCorrectIncorrect(false);
         }
      } else {
         toggleCorrectIncorrect(false);
      }
   }

   private void toggleCorrectIncorrect(bool isCorrect) {
      if(isCorrect) {
         lblIncorrect.IsVisible = false;
         lblCorrect.IsVisible = true;
      } else {
         lblCorrect.IsVisible = false;
         lblIncorrect.IsVisible = true;
      }
   }
}

As you can see, the little sample app loads in the Android emulator and runs as expected:

enter image description here

I have been using Visual Studio for many years, but until I started building this little MAUI project, I have never before seen phantom compile errors that don't prevent the app from running as expected. Additionally, sometimes these errors remain in the build output window, and sometimes they flash for a moment and then disappear.

What is causing these phantom compile errors and how can I get them to go away permanently?

HerrimanCoder
  • 6,835
  • 24
  • 78
  • 158
  • If the build fails and you force a run anyway then the default is to execute the last-good build. That's confusing so use Tools > Options > Projects and Solutions > Build and Run > "On Run, when build or deployment errors occur" = Do not launch. – Hans Passant Oct 08 '22 at 06:48

1 Answers1

4

Since .NET MAUI has a single project multi target platforms, this is probably happening because you are using some platform specific code (native to Android in this case) in a cross platform code (MainPage.cs in your case).

When the compiler tries to compile your code for another platform other than Android, it will complain when it hit Android specific code.

You can try and use preprocessor Conditional compilation to explicitly specify which namespaces and code portion to include/ignore for each platform when there is native code involved inside a cross platform c# code.

#if ANDROID
using Android.Media;
#endif

namespace MovieDoo;

#if ANDROID
   MediaPlayer _player;
#endif
....

enter image description here


EDIT

Generally speaking if what you are trying to implement:

1- Already implemented by a "cross platform" library (nuget package) compatible with MAUI or an API in Essentials, then use it as a common code for all platforms.

2- If not you have to search and implement it yourself using native code like what you did with Android MP3 player, but you have to do it for each one of the targeted platforms.

According to your comment you are targeting iOS and Android, so either search if a MAUI nuget package for playing MP3 exists and simply use it OR you have to implement the iOS part by using preprocessor conditional compilation:

#if Android
using Android.Media;
#elif iOS
//iOS namespace
#endif

namespace MovieDoo;

public partial class MainPage : ContentPage
{
#if Android
   MediaPlayer _player;
#elif iOS
//ios "_player" declaration
#endif
   string _userID = "777";

   public MainPage()
    {
        InitializeComponent();
      _player = new();
   }

    private void OnAudioClicked(object sender, EventArgs e) {
#if Android
      _player.SetDataSource("https://samplelib.com/lib/preview/mp3/sample-6s.mp3");
      _player.Prepare();
      _player.Start();
#elif iOS
//iOS implementation
#endif
   }
...

Related question

how to play an audio file - .NET MAUI

Cfun
  • 8,442
  • 4
  • 30
  • 62
  • I am not quite sure if it is the only problem, but wanted to highlight this point. – Cfun Oct 07 '22 at 23:43
  • Thank you Cfun. I want to target both `iOS` and `Android`, but the `Android.Media` lib was the only one I could find to play mp3. I thought the whole point of MAUI was not needing to go platform-specific, but having common .NET abstractions that would compile down to all desired targets. How can I programmatically play an MP3 (aimed at a remote URL) in a multiplatform fashion? – HerrimanCoder Oct 08 '22 at 13:17
  • Please check my answer edit, as for the MP3 player please ask in a new separate question focused on that matter only (a part from compiler errors) since this one was only focused on compiler errors. – Cfun Oct 09 '22 at 00:17