问题
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:
- Do the
Command
and theClicked
event execute synchronously or asynchronously? - Are there any adverse effects to using this approach.
- 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