>> Welcome back everyone to the Xamarin Show.
I'm your host James Montemagno and today we're going to
do a little Xamarin Forms 101.
Today we're going to be talking about command parameters.
I previously did an episode about commands.
So, when your user interacts with something like a button,
it will automatically call your code and the code behind.
You probably want to go back and watch
that video and it's down there in the description.
So, give that a click first.
Else, if you're ready to go,
let's learn about command parameters.
So, like I said, a command is simple.
You click on something and it does something in your code behind.
It's a mapping from I did this,
go call this code.
But often you may want to pass in parameters into it.
So, you actually may get something from
a different control or you may want to just
simply pass in different data that maybe is bound in a property.
So, a good example of that is I might be entering
my name and I want to use that as
a parameter that is passed in to query
for instance and instead of creating a new backing field,
you just pass it in as a parameter
and you can pass that to the command.
So, let's take a look at a code to see how we can
implement a command parameter really easily.
So, here I am inside a Visual Studio.
Now, this is my previous app.
So, it's a very simple little app that I can call it
increases the count and every time
I tap it, it increases the count.
If we look at the code here,
I have a button that has a data binding to increase command,
and then a label that will display count.
When I look at the view model in the code behind,
we can note that I have this IncreaseCommand right
here which is an ICommand and we've implemented a new Command.
But here, we'll call this method IncreaseCount.
When I do that, guess what,
it increases the count and tells it to
update the display count. Pretty simple.
But now let's go ahead and update this.
Let's say that we want to pass this as a string
of I for instance into here.
We may want to do something such as, hey,
whenever I increase will increase it by a different amount.
So, if int.tryparse and I'll
do I and then I'll say out int int two.
If that's successful, then now let's go
ahead and increase that here.
So, we'll say instead of plus plus,
we'll say that count plus
equals here int to two since we're passing in a string.
Now, we'll notice that
our command is very upset at us because it's no longer a void,
it is taking you in a string.
Luckily, the Xamarin Forms built-in command is also parameterized.
So, here instead of just a command,
I'll say it's a command of string.
So, now it is expecting if we look
here in action with string as the perimeter time.
Just super nifty.
I don't even need to change the ICommand here.
If I wanted to, I could say Command sub-string.
If I was going to call that from the code behind,
that's the same exact thing.
It just maps. So, you can think about what you want to do there.
Now, let's go into our main page and pass some information.
So, previously, I was just calling
the command and saying IncreaseCount,
but here we'll say Command parameter and we'll pass it too.
Now, you may be wondering, well,
how come I don't just pass
an integer or why am I passing a string?
Well, it's a good question.
In general, each different control may
pass different values and by default,
the object is going to be a string as is parsed out.
You can also use value converters or
other items here if you wanted to,
but we're just going to do the int try
parse because that seems to work well for us.
So, now we have our display count.
We're good to go.
Let's just go ahead and run this application again.
Here again we have that command parameter of
two going into my account here.
We can go ahead and see we have the string command,
parsing it and try parsing it.
Now, we should go ahead and see the new application
increasing by two every time.
So, here's our app that is started up.
Give it a second here. There we go.
So, now we have IncreaseCount and every time that I tap on it,
we'll see that it increases to two, four,
six, eight, 10 to
every single time because it has now passed that parameter down.
Now, you may be saying, well, I might be calling this for
my code behind and everything is Xamarin.
Well, don't worry. You're calling it from your code behind.
You can still pass parameters.
So, for instance here I have
my code behind for my main page and I'll just say VM,
which is my main ViewModel,
and then I can say IncreasesomeCommand.Execute.
Now, here it's going to go ahead and say, what's your parameter?
So, I can say three here for instance and every time I could
execute that from the code behind. There you have it.
A little Xamarin Forms
a 101 passing parameters to
your commands with just a few lines of code.
Hope that you found this entry in
the Xamarin Forms 101 mini-series helpful.
Leave comments down below.
Go and click some documentation links in
the description and make sure you hit that subscribe button
on YouTube or Channel nine to get the latest and greatest
Xamarin Show right in your inbox. Thanks for watching.
>> [MUSIC].