Accessing Native Platform APIs in .NET MAUI

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
Google oh hello there so you want to talk about accessing native platform apis in downtown Maui well don't worry there's several ways of doing it including using some of the built-in apis that have already been abstracted into a single beautiful API to do things like access connectivity geolocation device sensors and so much more but sometimes you might need to go a little bit further and that's when you need to tap into those native apis of each platform to do something really special now there's all sorts of different apis and you have access to them from c-sharp there's certain ways you may want to architecture code to make it work even better with dependency injection or if you want to just use conditional compilation and today we're going to break down everything you need to know about accessing those native apis in Maui so tune in [Music] thank you hey everyone I'm James and today we are talking about one of the most important parts of creating native cross-platform applications for iOS Android Mac and windows which is not only creating those app bundles and packages at the end of the day that you're going to deploy to the App Store but tapping directly in to some of those apis that are so essential that make those different operating systems a delight to use and can really unlock different capabilities for your application to expose your users for example I use Bluetooth and connectivity and a bunch of device sensors in my my Cadence application that connects to Bluetooth sensors and show you how fast you're running which is really really cool now there's tons of apis that are even just on a single platform so for example things like AR kit or you know iMessage kids those message kits and all those reality the crazy things that are happening on the different platforms you may want to add platform specific functionality just to your iOS application is a good example so there's different ways of thinking about how to do this and how to use some of the built-in dependency injection and how to architect the code to make it even better so let's check it out now first things first let's talk about some different ways of accessing some platform apis if you go to the.net Maui documentation under platform integration there's a bunch of good stuff over here specifically there's things about invoking platform code which we're going to talk about today but also configuring multi-targeting for shared projects but you can look at all of these different device features that are built in automatically into dynamawi if you go into overview you can take a look here things like app actions app info launcher main thread opening maps permissions which we've talked about before version tracking networking phone dialer SMS battery device display flashlight geolocation vibration text-to-speech clipboard file Pickers all this stuff it's all in there automatically for you now just because the API is in there doesn't mean that all of the functionality is exposed and that is because the team has done their best to abstract these API into a single you know API for you to use however if you need to tap into some specific things that aren't in there there might be a library already today on New get available if you head over to awesome.net Maui on Javier's GitHub you can actually browse through tons of samples over here which are cool Workshop Tools UI kits that are available and tons of plugins too there's tons of stuff in here so for in-app billing for example that's from me you can actually take a look at that and see hey this has been updated and here's how I can do in-app billing purchases it's already available right you don't need to write the platform specific code because someone's already done it for you either the Don Maui team or someone amazing community members out there so let's first look at the platform integration let's say we did want to go in and access some of these things for example uh let's look at networking connectivity right here what we'll see is that inside the documentation there might be a little bit of setup code required on the different platforms which is nice here on Android you need to at least have the access network state and then down here it's going to tell you exactly what you need to do to check connectivity so let's just literally copy this and put it into our code so let's go into my application I have a simple dependency injection sample I've been using a lot and let's go ahead and create another method down here so under our permission check let's do a relay command there we go I'm going to say um this will be a task and I'll say check internet there we go I'm just going to go ahead and paste that in there so if you have internet we're totally good to go um else we'll say we don't have Internet so let's go ahead and do this and let's do a pop-up so I'll say oh wait uh shell dot current dot display alert and I'll say you have Internet yay I'll say congrats that's pretty good and I'll say okay else let's just go ahead and then display the current status here so instead of saying uh you have internet you say check internet and then here I'm going to go ahead and say access type and I can say okay I'll say current current status there we go perfect all right so what's happening here well first and foremost we want to double check here is that there's coming from the connectivity API and this is inside of Microsoft Maui networking so if I came in and I say Microsoft dot Maui dot there's a bunch of things things like accessibility application model that has things such as app actions app info app theme app browser um all sorts of different things in here data transfer all these different things which is really really cool and what that is going to do by default is access the current Singleton of connectivity so that's one thing that's going to happen automatically for us so we can see that it's implementing icon activity so this is one way of doing this so if I came in and added a break point here and just launch it on my Android emulator we're probably gonna actually need to add a button to go ahead and check internet so let's go into our main page over here all right and let's go down to the bottom and I'll say check internet check internet this will be check internet command that was created for us automatically and just like that we've added another button and now we're going to go ahead and see this light up on our Android device and again this is just handling Singletons for us automatically and this relay command is coming from the.net Community toolkit for mvvm which I've done a video about up over here before so I'll go ahead and do that so let's give our application a second here to compile up and then redeploy all right our application is loading up and what we'll go ahead and see is that we should have that check internet button available after hot reload initializes there we go and I'm going to say check internet and sure enough our access type is internet which is quite cool now there's other things on this Singleton so we can see things like connectivity profiles I could also register to see what kind of when things change as well so that's an option and here what we're going to do is see a pop-up that's all you have internet that's great so now you're probably saying James well I don't want to just use that Singleton that's just around how do I register that with my dependency Injection Service well it is iconnectivity so what's nice about that is we can go into our Maui program this is what we have all of our Pages all registered already so we have our view models our Pages here and we can register a new Singleton so I'm going to say Builder Dot and then Services dot add Singleton now here this is going to be eye connectivity which is nice and I have two options that I can pass in the first thing I can do is just pass in an implementation the other thing is I can pass in an implementation Factory so if I just do connectivity dot current that's going to go ahead and do it and automatically register everything that I need now if you have read Jonathan Pepper's amazing blog post on performance you know that you actually may want to pass in a little function here to create it on demand so there's also a nice way of doing this it kind of lazily loads that up automatically for us so definitely take a look at that blog post which I'll link below so now what we need to do is go back to our main view model and up top in our Constructor I'm going to pass an eye connectivity and say connectivity and then we'll just use it back here as well so here I was going to go ahead and copy this in just like this then I'll say this dot connectivity equals connectivity and now what I can do below is instead of using the connectivity.current I'll say connectivity just like that so that's really nice when you want to use dependency injection that's built into Dynam Maui and of course that's not going to be unit testable as well if you want to inject your own eye connectivity when you're doing unit tests for example there okay so now our application is loaded up again and I can add a breakpoint here just like this let's go ahead and do check internet and sure enough I still have my connectivity implementation and my network access is internet and we're totally good to go awesome so that's really really nice so now we are using those built-in apis of Don Maui to access platform specific capabilities and there's tons of stuff built in but what happens when you want to access things that aren't exposed through Don Maui's API you want to tap into iOS Android Mac and windows specific code can you do that yes yes you can do that of course there's great libraries that do a bunch of this stuff out there but what if you want to just write it yourself you can do that which is cool so let's talk about how we would go ahead and Implement that into this application so let's head back over so instead of doing this display alert let's say I wanted to do a little toast pop-up specifically on Android there's nice little toast that pop up so especially if you have internet you know maybe I don't want to display an alert I just want to do some toast well there's a few ways that I could do that the first thing to remember is that dynami applications are multi-targeted which means that I have access to Android specific apis in fact if I choose a different Target here I'm going to have access to those apis so if I was to say toast we can see that it's in the android.widget namespace so if I say toast Dot make text I could then give it an Android context I could give it a string to display and the toast length as well now the cool thing here is that the context is actually on a built-in API called applicationmodel.platform dot current activity and then additionally I could say internet I have and then I could say toast duration or toast length I should say dot long and then I'll say dot show just like that so here that's one way of just accessing that capability now this is going to work all right except for if we scroll up there's this Android widget which again is going to work fine until I try to go to Windows over here and now we can see Android's not there because I'm trying to access Android apis and short codes there's two ways to get around this one is I could come in and say if and use a conditional compilation and say if Android and if only bring in that namespace and the same thing down here I'd also have to say if Android then do this end if there and you can see if I do if there's things like Windows iOS you can check platform version numbers ahead of time things like that which is really really cool so here this is going to be a blanket statement for anything Android now this will compile up and I'll be super happy because if I flip back to Android visual studio knows to light up that specific capabilities so I go ahead and run this again and it's going to be super happy and everything's great at the end of the day for my toast pop-up so here we go our application is booting up and loading for us and now since I do have internet we're going to see a toast that automatically gets hit in fact I'll add a break point here pull it back up check internet sure enough toast and there's my little toast ihat internet I have down there right so that's cool so if you're in a pinch and you need to spice up a little bit of platform code you can totally do that that's probably not exactly how you want to do it because that's not unit testable and of course you got to do this additional compilations so let's go ahead and remove this code I'm just going to go ahead and cut it and I'm going to go ahead and move this here so again we're going to keep our view models clean so what I'm going to do now is I'm going to go into my helpers class and I'm going to say add new item in this case we're going to add an interface into my code you don't have to have an interface technically but we're going to create one just because and what we're going to do over here is I'm going to say I toast as an option so I toast there we go now we're going to go ahead and clean this up so we're going to remove this stuff there we go and let's just go ahead and give it a void and I'll say make toast and I'll say string message so very simple one little interface method to implement so now what I'm going to do is I could for example just say if Android write the Android code here that's actually kind of cool in a way and Implement Ito so if I wanted to and say if Android here I could say and if and then I could say um you know um public uh class toaster for example I toast and then pull this code in just like that oh sorry we'll go ahead and implement this there we go implement the interface and go ahead and put put it in there like that would totally work right and again I'd have to do some conditional compilations and you know if Android and if and sure enough that would totally be valid uh there the other thing I could do at this point is I could say else and then I could say public class toaster again I toast here and I can say if I'm on like iOS for example let's just go ahead and implement this here we go and then not throw anything to say don't do anything on the other platforms I say end if and now we've basically implemented a toaster on our single class here so that's actually kind of cool so here I have everything inside a one source file it's kind of nice and easy especially if it's really simple you keep it all together and it's still doing a testable right technically we have all this all here and I can now go into my Maui program and again I could then say Builder dot Services dot add Singleton there we go and I'm going to say I toaster right toast I should say and then here I'm going to say um e and I'll just say new toaster there we go just like that so now with this we could go back into our main view model and I'll say you guessed it I toast and I'll say toast and then here foreign perfect there we go our main view model toast and toast cool now this is going to be a little upset at me because I toast is in public so let's go ahead and make this public over here just for good measure there we go awesome and now what I can do is instead of going off and doing a pop-up I can say toast Dot make toast and I'll say you have internet cool that's kind of cool right so everything's in this class we can have our eye toast we can go ahead and see here that I am going to run this on Android so this should be called automatically for me again all the other um things will compile for me and then it is you know relatively unit testable at the end of the day because I am passing in this toaster here which is kind of nice so it's kind of cool all right here we go so our application is running up check internet making toast and sure enough toast is displayed on the page internet I have perfect okay but then you're saying James maybe I don't want it in one single file what do I do okay okay okay so here's what I probably actually do is instead of having this conditional compilation what I would do is I would take this toaster class and I would come in to my Android folder inside of platforms and this is where I can access Android specific code here I would say give me a new class I'm going to say Toaster there we go and you guessed it I'm going to go ahead and put this in here do this here and now we have this nice little toaster here all right so use my maui.helpers and we're totally good to go okay now of course that's not going to be available on the other platforms so what I'm going to need to do is take this toaster class and put it in the different platforms so I could just you know leave it there and it'd be available but probably I'm going to implement this into the other places so I'm going to say Toaster here again we're going to come in here paste this in it's not going to do anything right now which is fine and then we're going to go ahead and put that in there and you guessed it we're literally going to copy and paste this toaster around into my other things so here this would be obviously Mac Catalyst and then this one here would be a window so the same would be true for Windows if you're doing it or for tizen if you're doing tizen 2. so now what this means is inside my eye toast it's nice and clean right just an interface nothing special that interface can live anywhere if we go back into my Maui program well now we have a problem right because actually our toaster lives in a bunch of different things it lives in Maui app di dot platform so you're like oh what do I gotta do what do I do I gotta come in and then you know return a new toaster but then you know only technically on one of the platforms so then I gotta say like if Android else and then I gotta you know do uh a long one like this and I got to go through all of them that seems not great James true so that's where namespaces come in and come in to be honest with you so instead of having to do conditional compilation we want to keep this toaster here we want to new it up so how do we make sure that these are available everywhere well if you remember back in our toaster classes inside of Android and iOS this thing has created a namespace called Maui appdi.platforms.android so what if I just did Maui apti Dot platform implementations for example and what if I use that exact same namespace in all of them right this kind of makes sense I have the same exact namespace everywhere now when I go and do a quick reformat I just have to bring in that one namespace which is my platform implementations so if you keep the namespaces the same remember that code all gets compiled up so if I toggle now to iOS and I go and I right click and go to definition I'm inside my iOS application if I go back over into my Maui program and I switch that to Windows sure enough I'm over on Windows you know what I mean so that's actually really really cool so as long as you just kind of keep this naming convention the same keep your different name spaces correct and Implement them there you're going to be totally good to go so let's run this one more time and now my source code is going to live inside of my Android platform source code here just like this I can go ahead and check internet which again I still should have internet over here I'll check internet sure enough it's there and we can continue on and I totally have internet we're really good to go we did it okay so that is almost the last way of doing it to be honest with you so that's one way of doing it inside of your application model now in another video what I'm going to be doing is talking about how to create reusable libraries that you can package up into nugets so stick around hit that subscribe button and ring that notification Bell so when I put out new videos here on my channel you can see exactly what we're talking about here this one's gone a little bit too long but I hope that you've enjoyed and if you had hit that like button down there that actually helps the YouTube algorithm and recommends a video to other people probably found this insightful not only can you just access platform apis from Donna Maui because they already have a bunch of apis for you you can access them through conditional compilation and of course you can use the built-in dependency Injection Service and access code in those platform folders or through those different conditional compilation lots of ways of doing it what's right well it's up to you it's your application I have many ways that I like to do it you can do it with an interface without an interface in the source code without server it's up to you like it's totally up to you this is the method that I like on this last one here of sort of putting in each folder but that's a James thing let me know what you think are there other ways that you're sharing code across your applications leave comments below and of course if you have a favorite Library that's out there I'd love to hear about it do a video on it so leave comments below as well anyways again I hope that you really enjoyed this video I hope you give it a thumbs up down there what other topics you want to hear Let me know I love to make some additional content on them really appreciate it helps you have the most amazing day ever thanks for watching foreign [Music]
Info
Channel: James Montemagno
Views: 14,410
Rating: undefined out of 5
Keywords: .net, .net maui, dotnet maui, platform apis, access ios apis .net maui, platform apis dotnet maui, platform apis .net maui, android apis .net maui, windows apis .net maui, clean code, interfaces, dependency injection, learn .net, .net maui tutorial, dotnet maui getting started, learn .net maui, learn dotnet maui, .net maui community, .net maui essentials, .net maui getting started, .net maui crash course, .net android, c#
Id: JCgxK0pWjNg
Channel Id: undefined
Length: 23min 51sec (1431 seconds)
Published: Thu Oct 13 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.