I'm working on my windows-phone app wich uses some simple data binding. I've allready created a app wich was based on the MvvM programming method.The app i'm curently working on also works by MvvM method. Because i want to keep my code behind as clean as possible i was looking for a way to make the "button click event" (wich normaly takes place in the codebehind page) take place in my viewmodel or mainviewmodel.
I have searched the internet in need of a simple explanation for the Icommand interface because i believe thats the way to go. Problem with the explanations i found was that some of them were based on MvvMlight toolkit using the CommandRelay function. I don't want to use the MvvM light toolkit because i want to understand things myself first. The other tutorials i found were written by over enthousiastic developers wich give you an overkill of information.
So basicly. Can someone show me the Most simple version of an Icommand bound to a button works?
In your XAML:
<Button Content="My Button" Command="{Binding MyViewModelCommand}" />
In your view-model:
public class MyViewModel
{
public MyViewModel()
{
MyViewModelCommand = new ActionCommand(DoSomething);
}
public ICommand MyViewModelCommand { get; private set; }
private void DoSomething()
{
// no, seriously, do something here
}
}
INotifyPropertyChanged
and other view-model pleasantries elided.
An alternative way to structure the command in your view-model is shown at the bottom of this answer.
Now, you'll need an implementation of ICommand
. I suggest starting with something simple like this, and extend or implement other features/commands as necessary:
public class ActionCommand : ICommand
{
private readonly Action _action;
public ActionCommand(Action action)
{
_action = action;
}
public void Execute(object parameter)
{
_action();
}
public bool CanExecute(object parameter)
{
return true;
}
public event EventHandler CanExecuteChanged;
}
Here is an alternative way to layout your view-model:
public class MyViewModel
{
private ICommand _myViewModelCommand;
public ICommand MyViewModelCommand
{
get
{
return _myViewModelCommand
?? (_myViewModelCommand = new ActionCommand(() =>
{
// your code here
}));
}
}
}
To add to Jays answer:
My all time favorite is the DelegateCommand from the Patterns and Practices team @ Microsoft. Check out this post for more info.
来源:https://stackoverflow.com/questions/12204099/bind-a-button-to-a-command-windows-phone-7-5