So you’ve spent several weeks successfully developing a Multi-Application UI app. Then, suddenly, your IDE shows that your application has hundreds of errors, and you cannot build. What’s going on?!
This post is an ongoing list of issues you might encounter while developing MAUI apps that seem to break your app for inexplicable “reasons”. Finally, I’ll suggest potential fixes to get you back into the development flow.
Let’s Get Started!
Everything Is Screaming Red and Broken!
If you were successfully developing your application and suddenly your solution failed to load, you likely have changed your environment. Failed MAUI solutions indicate a change in the .NET SDK.
For example, you may have been developing successfully using the .NET 7 SDK and wanted to try the latest .NET 8 preview SDK. Unfortunately, after installing the SDK, everything is broken! Ahhh!
The problem is the easiest fix among the issues I’ve seen with MAUI. To fix the issue, add a global.json
file at the root of your solution to tie your project back to the correct SDK band and to access the SDK band’s workloads.
{
"sdk": {
"version": "7.0.0",
"rollForward": "latestMinor",
"allowPrerelease": false
}
}
You may need to restart your editor, but you should be back in business upon restart.
Build Fails with One or More Invalid Files Were Detected
So you decided to import some new resources to pretty up your MAUI application. Unfortunately, you’ll get this error message after importing, and you’re confused. Is it my new files?
Microsoft.Maui.Resizetizer.targets(525, 9): One or more invalid file names were detected. File names must be lowercase, start and end with a letter character, and contain only alphanumeric characters or underscores:
Deleting the new assets doesn’t fix your issue, and now your app is perpetually broken. Aaaaaah!
Well, have no fear. The error is another straightforward problem with a clear solution. MacOS users may have touched the files in the asset folder, creating a .DS_Store
file that’s typically invisible in the solution explorer and Finder. However, you’ll see the file if you look at the Images folder using something like JetBrains Rider File Explorer.
Delete this file, and you’ll be back in business.
You can also resolve this issue by changing your MAUI project’s MauiImage
wildcard in the .csproj
only to include file extensions for known image types.
<!-- Images -->
<MauiImage Include="Resources\Images\*.{png,svg}" />
You can recreate the .DS_Store
issue by running touch
from the command line, which creates the offending file.
Pass SelectItem to My View Model Command
While not an issue, this one left me scratching my head for a bit, and I thought I’d save you the trouble of scratching your head. How do you bind a SelectItem
from a CollectionView
to a CommandParameter
?
If you’re using an MVVM framework, like CommunityToolkit.Mvvm
, you likely have a command on your view model that needs information from a user event like SelectionChanged
.
The trick to this solution is to add a name to your component, then use the Source
attribute to reference the item. It will make more sense when you look at the XAML.
<CollectionView
x:Name="TodosCollectionView"
VerticalOptions="Fill"
ItemsSource="{Binding Items}"
SelectionMode="Single"
SelectionChangedCommand="{Binding SelectionChangedCommand}"
SelectionChangedCommandParameter="{Binding SelectedItem, Source={x:Reference TodosCollectionView}}">
...
</CollectionView>
CommunityToolkit.Mvvm Can’t Find My Properties or Commands
CommunityToolkit.Mvvm uses source generators and requires an environment to read and generate the additional source code of your view models. The generated code includes properties and IAsyncRelayCommand
implementations. If you cannot find these elements in your code, you likely have one of the following issues in your code base.
You forgot the partial
keyword on your view model.
The name of your view model is not the same name as the generated command. For example, an OnAddItem
method with RelayCommandAttribute
will create an AddItemCommand
command.
Try cleaning your obj
and bin
folders, which might fix the current state of your project. By clean, I mean delete them from your disk.
Restart your IDE.
My MAUI App Never Starts! What Gives?!
If you’re using the dependency injection facility of MAUI, you must register all dependencies as part of your MauiProgram
file. Check your development environments console output for mentions of the following error.
System.InvalidOperationException: Unable to resolve service for type...
The solution is to register the missing type in MauiProgram
.
builder.Services.AddSingleton<AppShell>();
builder.Services.AddSingleton<MainPage>();
builder.Services.AddSingleton<MainPageModel>();
builder.Services.AddSingleton<TodoItemPage>();
builder.Services.AddSingleton<TodoItemPageModel>();
As a suggestion, you may want to create a convention to register pages and their corresponding view models automatically.
My MAUI Build is Slow!
If you’re running a build for every target framework in your TargetFrameworks
tag, you’ll get some slow builds.
<TargetFrameworks>net7.0-ios;net7.0-maccatalyst;net7.0-android;</TargetFrameworks>
In my case, iOS is the fastest build, so I remove a few target frameworks until I’m ready to work on Android.
<TargetFrameworks>net7.0-ios<TargetFrameworks>
If you want to put in the additional effort, you can use environment variables and MSBuild flags to create different builds for different situations.
The downside to this approach is you can inadvertently build something that works in iOS that is completely broken on other platforms before you realize it.
Conclusion
Developing with MAUI can be fun, but opaque errors are inevitable. I hope some of the problems and solutions outlined in this post can get you back on track.