Developing Mobile Apps — Question 6
You are developing a Universal Windows Platform (UWP) app that uses XAML and C#. The app must use the Model-View-ViewModel (MVVM) pattern.
The user interface (UI) triggers an event.
You need to bind the event to a view model method.
What should you do?
Answer options
- A. Create a custom behavior and attach the behavior to the UI element. Bind the behavior’s event trigger to the command declared in the view model.
- B. Create an attached property of type ICommand. Bind the UI element’s event to the attached property.
- C. Assign the value of the DataContext property to the view model. Use the BindingExression.UpdateSource() method to update the data source.
- D. Add a strongly-typed view model property to the view. In the code behind file for the view, invoke the view model method.
Correct answer:
Explanation
The correct answer is A because creating a custom behavior allows you to attach event triggers to UI elements in a way that adheres to the MVVM pattern, enabling separation of concerns. Options B, C, and D do not follow the MVVM principles as effectively; B involves using an attached property which is less common for commands, C focuses on data binding rather than event handling, and D involves code-behind, which violates the MVVM principle of keeping logic out of the view.