.NET MAUI - Less code with CommunityToolkit.Mvvm

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
today I will talk about community toolkit.nvm and the source generators that are there that makes it possible for you to write less code [Music] hi and welcome to a new.net Maui video today I will talk about Community toolkit and the mvvm package to be specific I will show you how you can use that to write less code because a community toolkit.vm has Source generators for generating stuff in your view models so that is what I will show you today so let's go and open Visual Studio and we will write some code here called user view model I set the properties data then we have a property and we have a backing field from each other and we get there and acceptor and we have one for each thing that we show when we have the command here so this Clause is 78 lines long just for a small page before I have used fold property changed to remove some of the boilerplate code that you need to write when writing view models then you for example just can use outer properties but with Community toolkit.vm you can instead using Source generator that are generating code live for the property chains generated code in compile time so in this video I will show you how you can use Community toolkit.nvm to make this view model contains much less code so the first thing we need to do is to install the nougat package so we do that we search for Community toolkit and you will find the community toolkit.ame with Microsoft as author so we select that one and add the package to our project so now we can go here to this View mode instead and write the same thing from scratch so what we need to do first is we need to make this partial class because all the generated code will be in another project class and to make this Clause Implement I properly changed as you can see that we had here I notify properly changed we can only add a simple attribute that will be observable object and you can see the namespace for it here so let's select that one and now we can go and create the properties and the only thing we need to do to create properties is to create a field and then the source generator will generate the property for us so we create a field with the name first name and now we remove the comment here and you can see that we have an error but if we do like this and add an attribute observable property the error is gone because the source generator have created the property for us so we don't have to care about and now we can create the other properties as well last name H is int and see it and now we can remove a comment there also and we have them and then we had the command as you can see here the me command so the only thing we need to do now is to create uh property is to create a method so in this case return void but we can also use a sync task and it will be async command if we do that but in this case we don't need it and then we just call the method me like this and now we also will create the property that we will use for is visible so private bull show message add the attribute and set show message to true like this and now it's important that you use the property and not the field because if you use the field it will not trigger proper change when you use it so if we go here to the saml we can see now we have a binding to me command because the generator will add command after the method name so it will be me commod in this case okay now we can go and try to run this up and see if this works so here we have that we have all the values of these labels and now we can try to tap the button yeah it works it updated the UI when we change the values so let's compare the user view model was 78 lines and this is only 43 lines so that is much less code in this view model it will save us time when we're doing view models and it'll be much funnier to code also when you don't have to write all the boilerplate code so this was a short video about Community toolkit.mub there are more stuff in the community toolkit.lm package but I can show you that in a later video so I hope this video helped you saving time when writing apps if you like it press the like button and please subscribe to the channel and we see you in the next.net Maui video
Info
Channel: Daniel Hindrikes
Views: 4,640
Rating: undefined out of 5
Keywords: .net maui, .net, dotnet maui, app development, mvvm, c#, csharp, .net maui tutorial, learn .net maui
Id: 86r52sv-gJs
Channel Id: undefined
Length: 5min 53sec (353 seconds)
Published: Fri Sep 30 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.