Calling methods in your View from your ViewModel using Action-Invokes

Executing ViewModel methods from Xamarin Forms Pages is easy, but how can Page methods be called inside ViewModels?

The First Prototype
3 min readApr 12, 2021

In Xamarin Forms, if you bind ViewModel Commands to Page Buttons, all of the logic from the button click action takes place in the ViewModel. The problem is that the ViewModel doesn’t have access to the Xamarin Forms controls, and so it’s difficult to access functions of Views from your ViewModel. Delegates are not commonly used, so I hope this article adds another tool in your arsenal that you can use to solve your coding challenges. Note that Action delegates can be used for all UI frameworks that use C#.

Example use case: Focus on an Entry

Let’s look at a scenario where we have a Password Entry and a Login Button on a Login Page. Our requirement is that if the password is incorrect, we want the password entry to automatically have the keyboard focus. Generally, validity of password is determined through the ViewModel using your Services layer, and that class is responsible for navigation if the password is right. But if the password is incorrect, and you want the UI to react to that error, it would be difficult to do it using traditional means.

Photo by ThisisEngineering RAEng on Unsplash

--

--