Week 8 Of 100 .NET Learning Challange: Exploring Music Integration and Native Code in .NET MAUI
Week 8 was an exciting journey of experimenting with music features in my .NET MAUI application. This week, I dived deep into implementing functionalities like importing, fetching, playing, pausing, and changing music tracks. Along the way, I encountered a number of essential features unique to .NET MAUI, which expanded my understanding of the framework's capabilities.
One of the most fascinating discoveries was learning how to seamlessly integrate native Android code directly into a .NET MAUI application. This opened up new possibilities, especially when working with platform-specific functionalities like scraping audio files from a device's storage. Additionally, I explored working with SQLite for local data storage, and to my surprise, the approach was quite different compared to Entity Framework Core in ASP.NET.
In this article, I'll share my experiences and key takeaways, including a brief comparison with React Native, where I found some interesting contrasts in coding style and workflow. Let's dive in!
Writing Native Code Into MAUI
Let's start with the most important thing first which is accessing native features. In my case, I have worked with importing music or files in general from storage. While doing this I have found out exactly 3 ways to work with native features, and I only had to work with 2 of those. The first one is starting with features integrated into MAUI that seamlessly work on all the platforms. In my application, that feature was importing files from storage. However, when it came to scraping all audio files from the device, it turned out, that unlike Android, IOS does not let an application perform that specific action. Hence, I had to write Android Native code in the .NET application itself! Isn’t that cool?! This is the second way, however, there is a 3rd way apparently where it allows developers to write native code inside it’s Android Studio solution itself using Java / Kotlin. Now didn’t I say we can write Native Code inside .NET MAUI itself, so what’s the difference here? Now there is a slight difference actually. The native code written into MAUI project is actually C# code that looks just like Android Native Code apart from the naming convention. There is so much resemblance that it is hard to recognize the difference. Let me show you guys a comparison. Since I don’t know how to write Native code myself, I am gonna take a little help from our good old friend GPT.
Working With SQLite
I also have worked with database and .NET official documentation recommended to use SQLite so I used just that. In this case, I would’ve been happier if would’ve let us use the EntityFrameworkCore which ASP.NET Core uses as ORM. At least there would’ve been a consistency. But on the flip side, I can tell why the implementation is different since a mobile app is not supposed to use databases that extensively. Although it is maybe possible to use EF Core I did not do thorough research and in this article, I am only trying to share my personal findings anyway.
A small code comparison with React Native
Before even starting this section, I just want to mention, I had to write significantly more lines of code compared to React Native which my small brain cannot comprehend why it was not possible for .NET team to make it shorter. Let me start with passing props for reusable component. Now let me show you a screenshot
What do you think from looking at the screenshot? They could’ve made it a liiiitle simpler don’t you think? And this is just one instance. I don’t wanna make this article too long and boring. This week I have also explored how to do a similar type of action like useEffect, which is achieved by observable onChangeEvent, and how to make reusable components which in MAUI’s case is referred to as ContentView (screen components are called ContentPage).
Finally, I would like to say that so far I felt this framework has potential. Although it still has significant lackings which are very very annoying. For example, namespace system in XAML is uughh. In my opinion they should properly work on their xaml first before bringing in Blazor support. Also, they should improve the workflow system, especially that hot reload thing does not work in 90% of cases. But hey, it’s still so so new in the game, these are totally acceptable. I remember, when React Native first came out, man it was baaaaddd!!
You can check out my latest progress here: 🔗 SparkleMusic-MAUI Repository. Also, follow my progress to my YouTube channel https://www.youtube.com/channel/UCz-5DWozqOkL__6BGCEb-2w where I try to post updates regularly.