Dot net Maui preview 12 is here introducing a whole bunch of new
features, including my favorite, which is a community contribution,
which is constructor injection.
When you're using shell.
That's right.
It's built in from start to finish including navigation, which many of
you have asked about, and I'm going to show you exactly how it works
today and how you can get started.
So tune in.
Hey everyone.
I'm James.
And, uh, back this time talking about .NET Maui, preview 12, preview
11 just officially came out not too long ago, but preview 12 is here.
It happens every single month.
There's a new preview.
This one doesn't have.
Found a mental crazy changes, but there's some really neat things built
into this preview and specifically constructor injection when using shell
now, shell is sort of an opinionated way of building out an application.
I'm really hoping that the team just, as it gets closer to
completion uses this as the default.
Uh, method with file new teaching it from start to finish.
I love it.
I did a whole video on it.
Why I love, shall I put it right here?
Uh, you can use shell today, uh, with, uh, Don and Maui.
You can just take a xamarinforms shell and just change a few.
Namespaces really easy.
Just go check out the documentation.
I'll put it down there below.
All right.
Here's why this is really, really cool.
I've talked before about how to use the built in sort of dependency
service of the host builders.
If you're used to asp.net core, it's the same dependency injection stuff, but
it's been a little bit trickier to do constructor injection for your view models
and under dependencies that you may have.
Um, so what I want to show you today is how you can just start using.
Uh, and new kind of architecture application without shell, but
then I want to show you shell and some of the new features as well.
So it's checking.
As always David has an amazing blog post over on the.net Maui and.net blog.
I'm talking about the new features.
There's a bunch of new stuff, specifically, brand new documentation,
all sorts of good stuff here.
Accessibility app startup data binding.
All sorts of good stuff.
Graphics, controls all sorts of, I mean, look at this.
I mean, there's all sorts of great stuff in here, so you can start
like, learning about the blazer web view, for example, and how to add
it into your application or build a new one, all sorts of amazing stuff.
So really happy to see this start to continue.
To roll out so people can start building apps with Don and Maui.
So other things that are important here are some Z index compatibility handlers.
This one's really important.
The dinette six unification of iOS types, Frank, and I'll be talking about
that on merged conflict in the future.
So definitely head over there, but if you're building older libraries that use
things like N float and endings and, and things like that, you probably need to do
some, some reworking of those, of those new get libraries or things like that.
Most people may not have to worry about them, but if you're doing a
lot of graphics, you may have to worry about that for iOS and Mac.
So check that out.
There's also this new extended toolbar over here.
They have a non shell they're gonna be working on shell soon, but more
importantly, actually, when you scroll down, um, there's a focus on shell.
I want, I want to talk that in a little bit and show you what this means, because
what it means is in the podcast app, you can do really cool things such as.
Constructor injection, which is really cool.
So let's first head over to just a file.
New project, nothing really crazy here.
Um, this is file new.
I made this app a while ago.
I'll put a link to it.
I think it's called like Maui app dependency injection or something like.
And it has labels and buttons.
You can click on them, but more quarterly is the code behind.
We have a main page that takes in a counter view model that we'd
want to constructor injection.
And then here we have a counter view model and it just has an eye command and
you can click on it and things like that.
So nothing really substantially crazy different over here.
Right.
We have this bondable object.
Code that was from like preview to that.
I've upgraded to preview 12.
Um, but, um, I haven't changed any of the core foundation because
if we look inside of the app, I'm actually going to inject the main
page into the application as well.
So that'll want to be injected in.
Now, if I go into the Maui Maui program, We can see that the use
Maui app is the code that probably is in your Don Moe application.
That's being injected in.
So what we have to do down here is add in these singletons.
So there's a single, uh, main page and a single counter view model.
Um, and when we build that up, it'll get injected in.
So for example, if I add a break point over here, and let's say at
a break point over here, We can see that I'm not Newing up any pages
at all, nothing crazy like that.
I am registering these singletons with the built independency
injection and sure enough.
This is what happens.
It gets injected into my application.
My main page gets injected in and I have to do anything at all.
Okay.
Now that is just built in dependency injection over there.
So for using like an activator or you basically want to grab.
Um, an item from that dependency service bucket, things will be injected
in that is constructor injection.
And this is very similar for reviews like prism or asp.net core, other
frameworks that have this built in.
It's really nice, especially when you're doing things like, um, interfaces you want
to inject in things into your unit tests.
This is really great, but let's take a look at a podcast
application because that uses shell.
So let me pop this down.
Yeah.
Specifically, the shell is a tab bar and we have things like a discover page.
We have a listen later page or subscriptions page over here.
So this is the shell with the bottom tabs of the application.
Now, previously, what you would need to do is take a look at,
let's say the discovery page.
And over here, it would have a discovery view model.
Now, previously I was creating it in the XAML.
Now I'm passing it in as a constructor parameter.
And if we look at the discovery view model, we can see that
now the discovery view model.
Has three things being injected, a show service, subscription service,
and a categories view model.
Okay.
So that is nice.
Previously we'd have to like reach into a bucket and grab it out and
have it inflated and all these things, but in no aware, am I
creating a new discovery view model?
Or am I creating a new show service or a new subscription?
Anything like that.
In fact, what's cool about this is if we look at this services extension, we
can see that I configure my service.
And on that builder, I add my blazer view model.
I had a Singleton, I had an HTTP class I'm actually doing, uh, different, uh,
switches here basically to add audio interfaces and, um, connectivity services.
And here we have different, uh, transient and singletons and
scoped services in here as well.
So all sorts of just services that I would use.
Now what I've done in the podcast app, which I'll link into the show
notes is I've added a view model extensions that registers every
single view model as singletons.
So I just have one of these view models.
Now you could sort of decide what's the scoping of the transient.
If you want them to be a single Tanner, you want them to be
a transient, create new ones.
Totally.
But here's, what's neat about this is that the Maui program now has our builder and I
could have added all those right here, but I have these little configure, extensions,
configure extension essentials, configure services, configure view models.
All right.
So this is my builder.
It registers everything inside of there.
And then it just handles it automatically.
So for example, if I put a break point here, Or a break point on
this thing, we can see it happen.
But I also want to mention, what's neat about this is that
it works with query parameters.
So here's the episode detail, for example, where I pass in via
a URL, the ID and the show name.
And what's cool is that it will pass those parameters, but also still
call my constructor at the same time.
And it will pass in all of my services here, which is really, really cool.
And also know that the discovery service also takes in another view model.
So it's cascading down the line of how these properties, um, and
these constructor parameters.
Are being injected into the application.
So let's go and pop this open.
Here's our podcast app.
And look at this, I go ahead and just look at this.
We have our, we have our shows.
We have our subscription service, our category services here, and sure enough,
our category services has a bunch of other stuff built into it too, down here.
Our discovery view model pipes in our view model right here, which
has all of those things on it.
And then our application pops open and sure enough, you're right.
Which is really, really cool.
And I can show you, um, exactly what one of those.
Um, URL's look like, right?
So here we have our category page passing it in a category ID.
So when you pass via URLs, it will also cascade down and do that constructor
injection for you automatically, which is pretty mind boggling.
And.
So that way, you're not just creating a new page, you're letting shell
handle the creation of those pages.
And then of course actually trickle down those query parameters at the same time.
So then it's up to you, right?
If you look at your view models here, do you want those to be singletons?
Should they be transient?
So they're creating a new one every single time, totally up to
you based on your application.
And if you've used ASP on that core before.
Then you should be pretty familiar with this as well, but now you have
a full application up and running and working all here a hundred percent,
uh, which is really, really cool.
All using constructor injection and a bunch of other things built into.net.
Maui preview 12 are, I didn't want to take too much time, but I wanted to show
you this awesomeness that I simply love.
I think it's so.
Unbelievably cool.
I'll put links to the blog post to my simple application resource and to
this.net, um, podcast application, which I've shown off quite a few times here.
Um, but I spent some time this week, updating it to Titania, Maui,
previ 12, with this new stuff.
And I think it's just.
I'm really excited for the future of where dotnet Maui is going.
If you have any questions at all, please leave a comment down there.
I would super appreciate it.
Uh, I'm really excited about all this stuff, as you can tell, I love it.
Um, and hope you catch me on some of my live streams.
I have some coming up, uh, usually on Fridays around 8:00 AM, right
here on YouTube and all the live streams are available too.
If you like this video.
Give it a thumbs up super appreciate that helps the channel helps
other people find the video too.
And of course, don't forget to subscribe because I come out with the new videos
every single week right here on YouTube.
So until next time I'm James and thanks for watching.
Ooh, I almost forgot.
One thing I wanted to mention is a lot of people have had
some issues with templates, kind of them not being up to date.
So one thing I wanted to tell you really quick, as I figured out how
to fix this thing, uh, with some help from John Dick over from the dynamic.
Uh, issue here is sort of, if you've installed templates free, a Maui check
or free the CLI and then installed visual studio, sometimes the CLI version is
older than what visual studio installs, but it is the default search location.
So it wins out.
So let's go.
Now you can tell what templates you have installed by going
into your user directory.
And there's this dot template engine folder.
If you go in there and see packages and you see a bunch of Maui stuff in here,
delete this folder, just delete it.
Okay.
Additionally, make sure you run this command.net new.
You dash you Microsoft and Maui templates.
This will go ahead and uninstalled the templates as well.
Do this all while visual studio is closed and then simply open up visual studio.
Now you're not going to remove the visual studio template because that is
sort of in a completely different area.
All right.
So when you go and create a new project, after you run these commands, you want
to make sure that you see the dotnet Maui templates with the dotnet Maui icons on.
So as soon as it loads up here, since I've run all these commands
and it's re re-indexing everything, what we should see, ideally
are the dynamic Maui templates.
1, 2, 3, you can double check this, of course, by coming into your
command line and going in, let's say CD to your desktop, make their test
12 CD test 12, and then do.net new.
And when you run this command, it should just create it and
it shouldn't be updated at all.
Didn't tell you there's an update at all.
Cause it will use the one that visual studio installed
that's way easier in general.
So anyways, that's how you fix it.
Cheers.