[英].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
....
In some cases you will have different separate files (classes/implementations..) for each platform Configure multi-targeting .在某些情况下,您将为每个平台配置多目标配置不同的单独文件(类/实现..)。
If you don't need to target a platform, then remove it by editing your .csproj
How can I remove the platforms I do not need to compile for in .net MAUI?如果您不需要针对某个平台,则通过编辑
.csproj
将其删除如何删除我不需要在 .net MAUI 中编译的平台? or uncheck it under project>properties dialog.或者在项目>属性对话框下取消选中它。 Also don't forget to delete the specific subfolder of
Platforms
.也不要忘记删除
Platforms
的特定子文件夹。
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
}
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.