Accessing Platform Features in .NET MAUI [7 of 8] | .NET MAUI for Beginners

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
>> [MUSIC]. >> Welcome back to another.Net MAUI Beginner Series Video. I'm James Montemagno, a Product Manager on the Developer Community Team here at Microsoft. Today we're continuing our journey, learning how to build beautiful native cross-platform desktop and mobile apps with.Net MAUI. Now, so far we've done a lot in our application. We've learned about what is in a file new project. We started to build a user interface in XAML. We added data binding in an MVVM architecture pattern, and we even did some navigation. Now next up is how to access Native API features. Now when we think about native APIs, things that are on iOS, Android, Mac and Windows, these are things that are platform specific. Now you can access these directly inside of C# thanks to the underlying systems of iOS and Android for.Net and Windows App SDK and UI3. Now what's great about.Net MAUI is that the team abstracted a whole bunch of common platform APIs into a single API, for you to use it as a developer. This means when you want to go access common things such as geolocation, sensors or connectivity, you don't have to learn a bunch of different APIs. There's a single API that you can use. Similar to the user interface, that abstracted common user interface controls into a single API, that's what these APIs do as well. Now what we're going to do is go back to our application and learn how to integrate connectivity into our application, which is a very common feature in nearly every single application. Let's head over there now. In this.Net MAUI Beginner Series we've been building the My Task application. It's a simple task application that allows us to add different tasks, also allows us to swipe to delete, or tap on one and navigate to get details and go back. All with the XAML user interface, data binding and the built in navigation system. The last thing we want to do is add some platform capabilities here. Now it would be pretty awesome to not allow people to add new tasks or synchronize them unless they have Internet connectivity, with.Net MAUI it's super simple to do that with the built in APIs that abstracts the underlying APIs of the platform. Let's go ahead and do that. Here again, we have all of our application that we've been building out. I haven't added anything to it, so if you're following along at home, you can just start coding along with me. Of course you can find a full final version of the app in the show notes. Here we have a main page and what we can see up top here is that we have a simple grid, an image, an entry, and this button called Add Command. Below we have a CollectionView, we have SwipeViews and we have our view models here using full MVVM. Now, if we want to get access specifically to different services or different APIs, there's tons that are built into.Net MAUI itself. For example, let's say we go up to the Add Command here. What we maybe you want to be able to do is check Internet here. So I can come in and say, Microsoft.Maui. Inside of here there are a bunch of different namespaces such as application model, which would give me access to different things like app actions, app info, launching the browser. Here's communication, for example, which would give me access to contacts, e-mails, phone dialers, whole bunch of things. There's also additional ones that we can see inside of here, like authentication. We of course have built in controls, devices. There's all things that are inside of here that you can get access to directly. Now, the one that we need access to is going to be connectivity. When I type in connectivity, we can see it's in Microsoft.Maui.Networking.Connectivity. Now associated with the connectivity is also an interface too. I could come in and just check the current access state here so I can say what is the current network access? But if you're like me and you want your view models to be testable, you could be able to past it a interface. Let's go ahead and set that up. I'm going to say IConnectivity. Here we go. Then what I'm going to do is simply fill this in. Of course, Visual Studio writes all the code for me, which is spectacular. Then what I'm able to do here is say if connectivity. and I'm going to say network access does not equal network access of Internet or then I don't have Internet and I'm going to return. What I can then do is say, await in here. We've gone ahead and modified this to say asynchronous, and we'll modify this to a task. I'm going to say Shell.Current.DisplayAlert and I'll say no Internet and say okay. Just with one line of code, I'm accessing the network access to see if I have Internet access or not and allowing me to add that new item into the list or not. If I don't, I'll pop up a message. Now, we need to register that connectivity service with the main application, and we can do that in the MAUI program. I'm going to do up top and say Builder.Services.AddSingleton, and I'm going to say IConnectivity, and then what I can do is I can pass it, the Connectivity.Current as what to use when it registers and resolves the IConnectivity. Now, I'm going to go ahead and show this on Android here, but it works on obviously all operating systems. There we go. What's great about the Android emulator is that I can flip on and off Internet here very easily. This is best done in non debug mode because the debugger does rely on some of the underlying network connectivity to actually debug your application. But here's my application running and I should be able to add Apples in there. There we go. Now if I go in and actually toggle on, for example, "Airplane Mode" that's going to toggle off the Internet and I'm going to go ahead and say Oranges and I'm going to add it's going to say, "Uh-Oh, No Internet." Just like that, we're using native APIs, but completely cross-platform in the same exact code that we have inside of our main view model to check Internet access. It's going to work 100 percent across iOS, Android, Mac OS and Windows. The last thing I want to do is head over to my Mac. We spent a lot of time inside of Visual Studio 2022 on Windows, but I took the same exact My Task application and I loaded them up on the Mac. You can go ahead and run your applications from Visual Studio 2022 from Mac or from the command line. Here we have the Mac and iOS versions of the app. Here you can add items, you get swipe to delete, you can tap and navigate and just like that, we have our full applications ready to go on both Mac, iOS and, of course, Android and Windows. There you've done it. You've integrated some platform specific APIs, 100 percent cross-platform, thanks to.Net MAUI. Now we're going to continue our journey in the.Net MAUI Beginner Series by taking a look at all of the other things that you need to know and resources to continue your learning journey. Stay tuned for more in the.Net MAUI Beginner Series.
Info
Channel: dotnet
Views: 35,134
Rating: undefined out of 5
Keywords: .NET
Id: gy7X1IZKeQQ
Channel Id: undefined
Length: 7min 54sec (474 seconds)
Published: Wed Jun 29 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.