Oh, hello there.
Welcome back to another video on my channel.
Today I wanna talk about how we can share source code across
our .NET Maui applications.
The question I get all the time, which is, Hey, .NET, Maui applications jam all
their source code into a single project.
What if I wanna share those stuff across?
Multiple projects.
Well, today I'm gonna show you exactly how to do that and some
things you may wanna consider.
So tune in.
Hi everyone.
I'm James and I'm back with another video This time, a very common question from all
of you, which is, how do I properly share source code across different applications,
and how could I actually take that source code and package it up into a new get
package and share it with the world?
Well, today we're gonna take a look at that.
Now, if you remember back in our exam and Xin Forms days, we would use things
like.net standard libraries to put all of our source code and then share it with
our iOS, Android, and Windows applic.
With .NET Maui, everything is inside of a single project.
Now, under the hood, it's multi targeting the different application
types, which means there is no Don Net standard or class library there.
It's just all in Android, all in iOS, or all in Mac.
And you can access platform capabilities through conditional compilation or
through adding source code into the platform folders, which I showed.
Last time in a previous video, and I'll put it up over there somewhere basically,
and how to access those native APIs.
But what if you want to create a reusable component, So like a
custom control that is shared across different applications, or you want
to create your own new get library.
That's a sort of a plugin model to access device capabilities
and share that with the world.
You may not wanna shove it directly into your .NET Maui application.
You may wanna create a class library for .NET Maui to do that.
So let's take a look.
All right.
Over here I've been using this Maui DI sample for dependency injection that
I've added like Bluetooth permissions to.
I've done this recent internet check thing, which actually does little pop
up down here on Android specifically, and it's really, really cool.
So I'll put a link to the source code below in the show notes as always.
But this little check internet pop up down here is a little toast.
So instead of doing a little popup every time I do this, it accesses the toast.
So here in check internet, we can see that when I click on that button, it
goes, it checks the connectivity built into .NET Maui that has internet.
It makes this toast, which is eye toast.
Eye toast is my own interface that has a single method on it called Make toast.
You gotta make some toast sometimes.
And then there's different implementations on Android.
On iOS, on Mac and on Windows.
Now these don't do anything because I just haven't implemented them,
but they live specifically in these platform folders, right?
Okay.
So what if I didn't want all of my sort of toaster implementations to
live inside of these platform folders?
What if I wanted to share it into other applications?
Well, we would create a .NET Maui class library.
So all I gotta do is right click on the solution, say add new project.
Now, there's two things that I want to note.
One is that you could totally just create a normal class library.
All right?
Here we can see class library.
This would just target do net six or even do net standard, and
you would add that exactly the same as any other project ever.
Forever, forever, forever, forever.
But if you wanna access platform specific capabilities, that's where
the Dyna Maui class Library comes in.
So we're gonna create.
I'm gonna say Maui app di dot shared.
That's what I'm gonna name it.
We'll target Don at six right now and I'm gonna add it in.
Now when we look at this, it's gonna look pretty similar to our .NET Maui
application with a platform's folder.
The difference is if we drop down, there's not all that extra stuff in it.
So if I go to the dropdown of here, we can see resources manifest activity types.
We also have a resources folder with all of our shared code in.
Not here.
Here, we just have simple class library implementations that you
could implement platform specific code in, which is kind of cool.
All right.
You also have a shared class, so this would be shared across all
of them, which is kind of amazing.
At the same time, if we double tap on the shared, uh, project
here, what we're gonna note.
Is that it is a multi targeted, This is gonna give us a done at six Android iOS
Met Catalyst, and in Windows One as well.
It uses Maui, which means I've access to all the APIs.
I have a single project here, and I also have implicit Usings enabled as well.
And of course I can go ahead and just simply set all the platform code there.
What's also cool is that I can come and I can actually hit this
pack button here and that'll create the new for me automatically.
So that's kind of, uh, spectacular.
It's gonna build it up and if I do nothing, , this will automatically
create a new get package for me.
So I actually go over into my code and go into my bin.
We can see there's my new get package.
It's like it's there already.
Like you can set a bunch of diff different properties on it.
But sure enough, I have a new get package that I could share, which is cool.
Okay, let's, let's go ahead and actually implement some code here.
Well, let's go ahead and actually put that toaster implementation in it.
So what I'm gonna do is I'm gonna go and I'm gonna move my eye toast down
into the base level here for shared.
And let's go ahead and delete that class one.
Now what I'm gonna do is I'm gonna go over into each platform and I'm
gonna drag and drop the toaster implementation into the Android folder.
Just like that.
And go ahead and see it, uh, up here.
There we go.
So now it's in here, toaster.
I'm gonna go over it.
Let's go ahead and pull this out actually, so y'all can see it.
I can now go into iOS, drag that down into the iOS folder.
I'm gonna go ahead and delete these as well from up here so I can actually like
go and get it, so I delete these toasters.
Good, Good, good.
Amazing, Amazing, amazing.
Let's remove that eye toast.
Good, good, good.
Cool.
And then now let's go ahead and move the uh, Mac Catalyst
Toaster down into Mac Catalyst.
Delete it from here.
Amazing.
And then let's move our Windows one down as well into our Windows folder.
Just like that.
Delete that there.
Good to go.
And of course if I had a ties in implementation, I would add it there too.
Now I'm gonna go ahead and delete these class ones.
And there we go.
So just delete that from there.
We don't need those.
And just like that, we have all of our toasters, right?
And we have our eye toast.
Now we have our name spaces here, so if we wanted to, we could clean
those up and put Maui app dot share dot helpers, for example.
We could do that there, but we have everything I need.
And of course, since we're using .NET Maui, as you saw inside the CS prod
means I still have access to those platform capabilities at the end of
the day, which is really, really nice.
Now I can, of course, still come up and I can still pack it.
You know, at the same time, which is cool, which means, you know, yet
again, when I go into my shared bin and debug, I have that package there.
Now packaging up those native capabilities, I could share
that on the internet to anybody, which is really, really cool.
But I wanna share it with this application.
So I'm gonna add it as a project reference, just like that.
And hit save.
And if we go into our Maui app, di we'll see at the bottom.
This is added a new item group with Maui Apdi dot shared.
All right, So make sure you save that there.
Go back into My Maui project and note that my toast are still available.
And if I click on it and go to definition, we can see that this toaster
is inside of our Maui app shared.
For example, if I go into my, uh, Android one here.
I can of course go into the toaster.
And now again, we hover over, it's inside of the Share project as well.
So when I compile this up, we're gonna go ahead and see in our
Android toast that I have this here.
We're gonna now have a fully decoupled class library using Dynamic Maui and
Android and iOS and Windows APIs.
That's shareable in this application.
But of course with the world now, that's part of a Noga package.
So it's actually really, really cool and, and I kind of like taking the
source code, the stuff that I'm not changing a lot and put it into another
library cuz then it doesn't have to recompile it every single time.
Right.
Cuz nothing will change in here.
Um, which is, which is quite.
So as long as you've gone in and you've added those different project
references, you're totally good to go.
And I do like that.
You know, when you add it to the main dependencies node, it
trickles down into everything else.
All right.
Let's give this a few seconds here to compile up and then get our source
code shared in this application.
All right, our applications up and running.
Let's check internet and sure enough, The current activity is the main activity.
So it all handles all that stuff elegantly for us.
Hit continue and sure enough internet I have, which is super du cool.
So that is really, really cool.
Being able to break away and create a shared library that can be TED
up and then of course put into your C I C D system automatically.
Now I do this with a bunch of my libraries.
Um, I have some that only target on a Maui, some that are
still touring, exam back and.
So you can take a look at some of those, like my in net billing plugin.
I'll put links to everything below that I talked about today, including this source
code sample so you can take a look at it.
Hopefully this answered most of your questions.
If you have more questions, I'd love to hear from you.
Let me know in the comments below.
And of course, if you like this video, I know you, I know, I know you know that.
I want you to hit that like button down there because that goes into that YouTube
algorithm of ridiculousness that goes ahead and recommends it to other folks
and maybe are looking for similar content.
And of course, Don forget to subscribe to the channel and ring that notification
bell so you get up to date info whenever I put out a video right here on my channel.
Ah, thanks so much for tuning in.
I hope this really helped all of you, um, sharing more code
and accessing those native api.
In that shared code as well and sharing with the world.
Love to see and hear the plugins that you're creating, so feel
free to drop a comment below.
Anyways, thanks again for tuning in.
Super appreciate it.
Have a one.