Build Your First Plugin for .NET MAUI with this FREE Template!

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
Have you also always dreamed of building your own .NET MAUI plugin? Well, let me make your dreams come true. In this video, we're going to see how to build your own .NET MAUI plugin, the easy way. If you've been coming to this channel more often, then you probably know what the main idea behind .NET MAUI is. With .NET MAUI, you are provided with a unified API that is an abstraction layer on top of iOS, Android, Windows, macOS and Tizen. So that you have this one unified API service built on .NET and C# that allows you to build cross platform apps for all these platforms. Each platform has their own set of APIs. And with an API, I mean all the objects, all the functionalities, all the visual elements together with their methods and properties that you can use to build your own app on that platform. And for each of those platforms, iOS, Android, Windows, all the rest, they have thousands, literally tens of thousands of APIs for you to use, which is truly amazing. But you can also imagine that it's a big, tremendous effort to try and capture that all in an abstraction layer, a unified abstraction layer that is .NET MAUI. And it's simply impossible, I would say, because some APIs are very specific, very unique to that platform. So it's hard to capture that in a cross platform context. And the other reason being that there's just too many APIs. And that is where there lies a great opportunity for you. Because the chances are that you will run into a time where you have a piece of functionality that you need inside of your app and you know it's available on a platform or maybe all the platforms. So there's a couple of things that you can do. You can go into your project, you can hack it in there and you will have it for your app. And you can just use it and shield it from the world and never show anyone. But where's the fun in that? So the other option that you have is be a good open source citizen and maybe abstract that functionality into a plugin and put it out there for the world to see and use it in other projects as well, and maybe score some internet points. That is what I've been doing at the time of recording. I have three plugins out there now. One for the speedometer, so basically your step counter, one for the screen brightness, and one for playing audio in your net Maui apps. And while setting up each of these plugins, I had this great naming scheme, if I may say so myself. Plugin, Maui, whatever the feature is that you're going to implement. But I also noticed that I had the same kind of project set up and structuring my code all the same ways. So I thought, why not abstract that into a project? A GitHub repository, template that is a thing on GitHub. So you can easily clone this project structure into your own account and then start building your Net Maui plugins in the same way. So let me introduce in this video how to build your own plugins. The easy way for net Maui. When you are ready for your first Net Maui plugin, then this is the place to be. This is a public repository template on my account. You'll find the link down below in the video description and I've named it Plugin Maui feature where feature is going to be the thing that you implement, right? Or you can just skip this whole name of scheme together if that's what you want. You can see it's marked as a public template and that means that this is available as a template and you are not really forking this or anything onto your account. But what this does is you will create an actual copy on your account and then you can clone it as a regular repository. It will be a regular repository from that point on and you can start working on your plugin as it's your own, right? So I don't have anything to do with that. I don't control it, I don't own it. It's just your repository. After that you can just use this as a blueprint for your plugin, for your repository. So that's what that means. Now I'm going to go over everything that's here in this repository. I will show you that in a minute. But I'm going to show you how to pull this into your own account. And you can start with this. So what you can see here is this green button with use this template in the top right. And you're going to click that and click then create a new repository. That's going to create a new repository on your account from this template. So whenever you do that, you can see you have to fill in a couple of things. You probably see a little bit different than this one here. You can specify the template and then you have to specify an owner. You probably only see your own personal account here and you can specify a repository name. So here you want to do Plugin Maui testing. Testing is going to be my feature. And if you do not want to use this naming scheme, do something else. But it has to be unique on your account. You can optionally describe your repository right here. You can also do that later and you can specify if it's public or private. I'm going to keep it private for now and I'm just going to create it. So it takes a little bit to create that repository on your own account. And then when that's ready, you can just start cloning this onto your machine and start working on your plugin. Obviously I prepared this already a little bit. So I already cloned this to my local machine and I opened it here in Visual Studio code. So you can see here all the files that are in this repository. So this is the Plugin Maui testing one, the one that I created from that repository template. And all the files that are in that template, the exact same will end up in your repository as well. So that means we have some work to do in terms of like replacing names and strings and all that kind of stuff. So I created this README markdown file which is right here, which has all the information basically that you need to know. Now this is the markdown markup file. So I'm going to click this example, well, the preview here so that it reads a little bit more nicely. So you can see, hey, we have the Getting Started and published to NuGet. You can do that as well. This template contains well, a sample which you can find in the samples folder. This is just a file new .NET MAUI project with already the reference to this source plugin right here. So that you can easily demo it to people that are stumbling on this repository and are like, hey, how do I use this API? And it's very handy to just open this sample solution and start working on that and build your app together with your library side by side. It makes it so much easier to test your functionalities. Right? So we have that. So the source I already mentioned that a boilerplate README file. So I already came up with a README that you can use for your library as well. You can find that right here. So it describes plugin Bowie feature available on NuGet API usage you can describe the API right here and do all these kinds of things. So go over this and see what makes sense. Make sure to remove this README where I describe what this repository is and how to use it with the README feature. So delete this one and rename this to README so that it will show up nicely in your GitHub repository as well. Right? So there's a couple of those things that you need to know, but this is still whoops I did something wrong now. So let's get this preview back. So this is all what is in here. There's also GitHub actions in here for continuous integration. There is a GitHub action in here for releasing it to NuGet. So it has everything to get started. Now here are the actual Getting started steps. So I tried to get it as specific as possible, but there is still some things to know. So there is some text here. Go over it. Once you know the steps, your next plugin will be super easy so you can create your own. We already did that. And you have to replace all the occurrences of Plugin Maui feature with the thing that you want to do. So for instance, plugin Maui Screen Brightness or Plugin Maui Audio? Now if you're using Vs code, that's very simple. Just copy plugin Maui feature. Copy that, go over to this search thing. We're going to search. We're going to replace it with this other thing. And you can see 59 results in 33 files. Replace all and you're mostly done already, right? So that's how easy it is. The one thing that you want to remember is that on the file system you will still have this Plugin Maui feature. It's not going to rename that. So you're going to have to rename this to the same thing that you replaced all the values with, right? So you have to rename this folder, this solution file, the CS proge in here and all these right. There's a couple of renaming things that you need to do to make it accustomed to your project. But that's how it works. If you're going to use this template, maybe if that's good for some people, let me know down in the comments if I need to create some command line tool commands that will make it easier to replace those things so that it will be replaced automatically with the name. Would that be useful? If you are getting started with this, please let me know down in the comments or open an issue on this repository. So we've got that. It even says it here. Like don't forget to rename the files and folders in your CS proge. There is like for the library, there is a bunch of metadata for when you're going to publish this to NuGet. So you probably want to look into that as well. It's here with NuGet authors. Replace that with your name. Copyright author, replace that with your name. This package URL should be renamed automatically whenever we replace everything from Plugin Maui feature. You want to have some tags in here. You want to have a title and description. So there's a couple of things that you want to go over and see if that makes sense for your library, right? So we have that all in there. So we have that. And the other thing while I'm at it here in the license, I've included the MIT license by default. You want to probably rename this author name to your name, right? So that it's licensed property and everything is okay. If you want to have it to have a different license, remove it here and put in the other license that you want to have on your library. Please have a look at the licenses. That is important stuff. So we want to do that. There is some other things here and you will figure that out for yourself. Like I said. Published in NuGet. It's all in here. A little bit more detailed as well. I'm sure you can figure this out. Now the one thing that I find important to highlight here is the project setup because it's a little bit different than the typical Net Maui project. Because here I added some extra stuff in the CS proge, which makes the platform specific code based on the file names right here. So you can see that right here in the tree, in the file tree we have this feature, Android CS feature, macOS feature, net feature shared, et cetera, et cetera. You can read it. And that's set up through this. Now typically you don't have to touch this. This is only sets up like, hey, whenever we're targeting iOS, then it's going to build all the files that have iOS in it, right? So it's going to do that and the same for Android. So in your typical net value projects, you have the platforms folder, and then under that you have the subfolders for Android, iOS, Windows. And it's going to compile that only whenever you target one of those platforms. I've changed that to be based on the file names right here, just because I found that structure worked better for me. Hopefully it does for you too. If you're going to use this template, else you'll have a bad time. You can just create your plugin on your own. But this is the way how I set it up. So usually what I do is I have this interface. So you can have this plugin interface and you can plug this into dependency ejection, if that's what you want or do other things with. It also provide relevant comments on all of this. So you will have this interface and all of these classes here will implement that interface. And this is all shared classes. That's just how it works on .NET MAUI. So we have this shared one first. The shared is, .shared.cs, will be compiled for all platforms. So that is shared code. And we have this public static class feature, right? So you can also use it as a static class like this. This very much follows how .NET MAUI essentials APIs work, right? So if you go to the geolocation one, you will see that you can use Igeolocation for dependency injection or you can just do geolocation default and then whatever you want to use. So this follows that exact same pattern. Then here you can start implementing your APIs, right? So we have this I feature and we're going to say, hey, I have this public string foo get set. So this is my interface. And now on all these shared classes I can implement that as well, right? I have this partial class feature. Implementation is what it's called. And then it implements IFeature So for Android, I want to have this public string foo and I have to do get set. But here it's going to return Android, right? So I can have my Android specific code right here. And actually while I'm typing this, I shouldn't have public here because this is an interface. So this is just like this. And now with a property, it doesn't really make much sense. But if you're going to do methods, then you can do like, hey, I want to have this void do thing. And I have methods right here. And again, I shouldn't specify this because I'm inside of an interface, but now I have to implement this for Android and I'm going to do public void do thing. And I can add this method here. And inside of here, I can do the Android specific implementation, right? So if I want to do that, I can now use the Android specific thing because there's Android APIs that I need to call for this. And then for Mac and iOS, I need to do the same thing because I need to now use iOS specific APIs, right? So that's how that works. And then in my sample, I can just go over here and we already have references to it. So in my main page, I can already see this if feature which is injected through dependency injection. And I can just say, hey, feature do thing. And that's the way I can very easily get started with these platform specific plugins for .NET MAUI. So we have all that in place and that's how you start working with it. Now, the hard part is that I cannot really show you how to implement your feature because that's something that you need to come up with and you need to implement. But hopefully this template will get you started with implementing plugins for .NET MAUI. I hope you got all that. It's a little hard to explain without going over the code that I actually want you to write or show what it's doing because that's the hard part. You have to actually come up with the feature and implement it. I hope it's very clear. I hope this repository template is useful for your .NET MAUI plugin development. Let me know what you think. If something should be different, let me know on the repository and we can work on that together. If you've built something, that's what I'm most curious about. Let me know down in the comments. But don't post a link. YouTube will delete that automatically think it's spam. So just post your username/Plugin.Maui.Feature and I'll figure out the GitHub.com that comes in front of it. And I'm very curious to see what you have come up with. And I'll be seeing you, of course, for my next video.
Info
Channel: Gerald Versluis
Views: 2,672
Rating: undefined out of 5
Keywords: .net maui, c# maui, what is .net maui, learn dotnet maui, learn .net maui, .net maui essentials, .net maui getting started, build maui plugin, .net maui plugin, maui nuget, build own maui plugin, build a plugin, maui nuget packages, c# maui api, maui tutorial c#, plugin.maui.feature, plugin.maui.audio, github template repository, github template repo, .net maui plugin template, maui plugin template, dotnet maui plugin template, maui plugin, maui plugins, dotnet maui, nuget
Id: ZCQrlGT7MhI
Channel Id: undefined
Length: 14min 38sec (878 seconds)
Published: Mon Aug 21 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.