I'm developing an Android application using Xamarin and MvvmCross. As seen in the layout posted at the bottom of this question I have a TextView and a Button.
I want to achieve the following things:
Bind the OnClick listener of the button to the
onClikCommandmethod as shown in the code below.When the
onClikCommandis called I expect the value of the Text attribute of theTextViewto change according to the evaluation of the if-statement.Broadcast the value of the evaluation via a cutomized EventHandler and EventArgs.
Concerning the binding part, I've read several tutorials and I found that some developers are using
ICommand interface and Command in the property of UI-Command,
and some are using
local:Mvx
My question is, what is the difference between both kinds of bindings and in which context either of them is preferred?
code_VM : IMvxNotifyPropertyChanging
public event EventHandler<ValidPlayValueEventArgs> ValidPlayValueEventHandler;
public ICommand onClikCommand {get; private set;}
public isValidPlayValue {get; private set;}
public VM() {
onClikCommand = new Command<string, string, string>(isValidPlay);
}
public class ValidPlayValueEventArgs : EventArgs {
public isValidPlay {get; private set;}
public ValidPlayValueEventArgs(bool isValid) {
isValidPlay = isValid;
}
}
public void isValidPlay(string p1, string p2, string p3) {
if (p1 && p2 && P3) {
isValidPlayValue = true;//<----I expect this to update/set value in the textview!! true??
ValidPlayValueEventHandler(this, new ValidPlayValueEventArgs(true));
} else {
isValidPlayValue = false;//<----I expect this to update/set value in the textview!! true??
ValidPlayValueEventHandler(this, new ValidPlayValueEventArgs(false));
}
}
Layout
<TextView
Command="{Binding isValidPlayValue}"
<Button
Command="{Binding onClikCommand}"
If I understand your question right you want to know the difference between:
and:
Both code blocks achieve the same thing but the difference is that the first code block is for Android applications and the second block is for UWP applications. Since you are creating a Android application you should go for the first option. I would expect that your Android application would not run when using the second code block.
Extra:
For the implementation of the functionality you described in points 1, 2 and 3 I would like to give you a tip:
Don't pass the value of your
TextBoxas a parameter toisValidPlay. Instead bind the value of yourTextBoxto a property in your ViewModel. Question: what is it that the parametersstring p1, string p2, string p3represent? I assume that you actually want to have 3 TextBoxes instead of one.An example of what your ViewModel could look like:
And your Layout could look like: