I’m attempting to improve my MVVM abilities in my next WP7 App (which is written in Vb.NET). I have a textbox that has been given focus and has a WP7 keyboard displayed. I am using command binding and a xyzzer’s bindable Application bar (which is excellent).
http://bindableapplicationb.codeplex.com/
I want to be able to cancel the focus of the TextBox from the ViewModel, by setting focus on the form. Normally (Non MVVM) I would do this from within the form by calling:
Me.Focus()
But I can’t do this from the ViewModel (and I shouldn’t). At the moment I am raising an event out of the ViewModel and catching it on the form but its nasty. Is there a MVVM Friendly way of doing it? I haven’t been using a Toolkit so far as there are limited examples in vb.net.
I have been using command binding.
Using Pedros example, and the other services I have previously implemented in my App I put together the following solution in vb.net:
Create an IFocus Interface, This Interface can be implemented by the focus service or a Mock
Create a IFocusable Interface. This will be implemented by the ViewModel and accepts an object that implements IFocusInterface.
Implement the Focus Interface with a singleton pattern
In your ViewModel Implement IFocusable, and make sure you pass in the Focus Service Singleton into the ViewModel after the ViewModel is Constructed.