Let me guess: the problem is that when you click the ApplicationBarIconButton, the TextBox hasn't updated yet the binded property on the ViewModel, correct?
Use the ApplicationBarBehavior from the Cimbalino Windows Phone Toolkit (you can get it from NuGet also), that handles that internally - so before the ApplicationBarIconButton click event gets done, it has already updated the TextBox.Text binded property!
Check the sample code in GitHub and you're all set to use it!
Edit:
If all you want is to set the focus on the page (and thus closing the keyboard after the TextBox looses focus), I'd go with an external class to do the job, and then use it in the ViewModel, something like this:
//This is the service interface
public interface IPageService
{
void Focus();
}
//This implements the real service for runtime
public class PageService : IPageServiceusage
{
public void Focus()
{
var rootFrame = Application.Current.RootVisual as PhoneApplicationFrame;
if (rootFrame == null)
return;
var page = rootFrame.Content as PhoneApplicationPage;
if (page == null)
return;
page.Focus();
}
}
//This implements the mockup service for testing purpose
public class PageServiceMockup : IPageService
{
public void Focus()
{
System.Diagnostics.Debug.WriteLine("Called IPageService.Focus()");
}
}
Then, on your ViewModel, create an instance of the service like this:
public class MyViewModel
{
private IPageService _pageService;
public MyViewModel()
{
#if USE_MOCKUP
_pageService = new PageServiceMockup();
#else
_pageService = new PageService();
#endif
}
}
And when you want to set the focus on the page, all you have to do is call _pageService.Focus()
.
This is a fully MVVM'ed way of solving the problem!