Reputation: 1217
I want to bind the back button of the navigation item and whenever user clicks the back button I want to execute some code binded to it in view model.
My current code:
View:
var bSet = this.CreateBindingSet<xView, xViewModel>();
bSet.Bind(NavigationItem.LeftBarButtonItem).To(vm => vm.CheckIfLoading);
//bSet.Bind(NavigationItem.BackBarButtonItem).To(vm => vm.CheckIfLoading);
ViewModel:
private ICommand checkIfLoading;
public xViewModel()
: base()
{
this.messenger = mvxMessenger;
checkIfLoading = new MvxCommand(DoRefresh);
}
public ICommand CheckIfLoading { get { return checkIfLoading; } }
private async void DoRefresh()
{
await Task.Delay(5000);
}
When ever I hit the back button, I want it to hit DoRefresh(), but its not. Can anyone point out the error or help me resolve it ?
Upvotes: 1
Views: 893
Reputation: 1239
You can use the following code to bind Clicked
to your ICommand
:
this.AddBindings(new Dictionary<object, string>()
{
{ NavigationItem.LeftBarButtonItem, "Clicked CheckIfLoading" }
});
Side note
The following line might not work if Clicked
isn't the default binding:
bSet.Bind(NavigationItem.LeftBarButtonItem).To(vm => vm.CheckIfLoading);
Other ways to bind to a specific property can be achieved by using the .For(labda)
method as following:
bSet.Bind(NavigationItem.LeftBarButtonItem).For(lb => lb.Clicked).To(vm => vm.CheckIfLoading);
But in this case that won't work since lb.Clicked
expects a left hand sided action ( +=
or -=
)
For more information about binding take a look at https://github.com/MvvmCross/MvvmCross/wiki/databinding
Upvotes: 3