简体   繁体   English

.NET MAUI 编译错误显示,但移动应用程序仍然运行

[英].NET MAUI compile errors display, but the mobile app runs anyway

I have a .NET MAUI project that shows these 3 compile errors (that shouldn't occur), but it compiles and runs successfully anyway:我有一个 .NET MAUI 项目,它显示了这 3 个编译错误(不应该发生),但它仍然编译并成功运行:

在此处输入图像描述

Here is the source of my MainPage.xaml.cs :这是我的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:如您所见,小示例应用程序加载到 Android 模拟器中并按预期运行:

在此处输入图像描述

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.我使用 Visual Studio 多年,但在我开始构建这个小 MAUI 项目之前,我从未见过不会阻止应用程序按预期运行的幻影编译错误。 Additionally, sometimes these errors remain in the build output window, and sometimes they flash for a moment and then disappear.此外,有时这些错误会保留在构建 output window 中,有时会在 flash 中保留片刻然后消失。

What is causing these phantom compile errors and how can I get them to go away permanently?是什么导致了这些幻影编译错误,我怎样才能让它们永久消失 go?

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).由于 .NET MAUI 具有单个项目多目标平台,这可能是因为您在跨平台代码(在您的情况下为MainPage.cs )中使用了一些特定于平台的代码(在本例中为 Android 本机代码)。

When the compiler tries to compile your code for another platform other than Android, it will complain when it hit Android specific code.当编译器尝试为 Android 以外的其他平台编译您的代码时,它会在遇到 Android 特定代码时报错。

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.当跨平台 c# 代码中涉及本机代码时,您可以尝试使用预处理器条件编译来明确指定每个平台要包含/忽略哪些名称空间和代码部分。

#if ANDROID
using Android.Media;
#endif

namespace MovieDoo;

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

在此处输入图像描述


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. 1- 已经由与 MAUI 兼容的“跨平台”库(nuget 包)或 Essentials 中的 API 实现,然后将其用作所有平台的通用代码。

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. 2- 如果不是,您必须使用本机代码自己搜索和实现它,就像您对 Android MP3 播放器所做的那样,但您必须针对每个目标平台执行此操作。

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:根据您的评论,您的目标是 iOS 和 Android,因此要么搜索用于播放 MP3 的 MAUI nuget package是否存在并简单地使用它,要么您必须通过使用预处理器条件编译来实现 iOS 部分:

#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如何播放音频文件 - .NET MAUI

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 .Net Maui 无法编译新的 macos 项目 - .Net Maui cant compile a fresh macos project New.Net MAUI App 项目抛出“名称‘InitializeComponent’在当前上下文中不存在”构建错误 - New .Net MAUI App project throws 'The name 'InitializeComponent' does not exist in the current context' build errors 在 .NET MAUI 应用程序中使用具有多目标的 PLatformBehavior 时,当前上下文中不存在 GetValue 和其他错误 - GetValue does not exist in the current context and other errors when using a PLatformBehavior with multi targetting in .NET MAUI App 如何在 .NET MAUI 项目中显示 DatePicker 弹出窗口? - How to display a DatePicker popup in .NET MAUI project? .NET MAUI 从弹出窗口显示弹出窗口 - .NET MAUI display a popup from a popup .NET MAUI Blazor 应用程序拖放不可能 - .NET MAUI Blazor App Drag and Drop impossible 使用.Net Maui 中的进程运行外部应用程序 - Run external app using process in .Net Maui 自定义渲染器导致 .Net MAUI 应用程序崩溃 - Custom Renderer causes crash of .Net MAUI app .NET MAUI App中有没有PDF Viewer? - Is there any PDF Viewer in .NET MAUI App? 当我尝试在 xamrin 表单中添加 ZXing.Net.Mobile.forms 并进行编译时,它显示错误 - When i try to add ZXing.Net.Mobile.forms in xamrin forms and compile then it shows errors
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM