How do `Clicked` and `Command` work when they are both used on the same Xamarin view?

三世轮回 提交于 2020-06-01 05:58:50

问题


I have a Command (in my viewModel) and a Clicked event (in my code behind) attached to the same view in my XAML, and I am noticing some weird results. I am doing this because I would like a view to receive focus right after the code for the command in executed:

  1. Do the Command and the Clicked event execute synchronously or asynchronously?
  2. Are there any adverse effects to using this approach.
  3. Is there a better way I could handle this?

回答1:


Yes, the Click events are executed asynchronously. However, I believe the commands are not. They are executed right after the associated action being performed.

My suggestion would be not to use these both together for what you are looking for. Because events will be handled whenever the thread and processor is free. So you cannot rely on it for your requirement as it depends on some other action.

You might achieve what you want by using the Behaviors in Xamarin forms. Refer below links for more details.

https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/behaviors/creating




回答2:


A Xamarin view is a partial class - Separated by View and Code behind

  • View(use XAML to create front end)
  • Code Behind (write logic)

you can easily write an entire app using View and code behind but it's not practical in the long run.

If you want to use MVVM or MVC you will need to use Binding (separate logic from view )

to answer your question :

Commands - best when using MVVM or MVC

Click Events - Best for code-behind logic e.g. click a button

More information about MVVM design patterns



来源:https://stackoverflow.com/questions/59144365/how-do-clicked-and-command-work-when-they-are-both-used-on-the-same-xamarin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!