Windows Phone 7: Saving Data when Keyboard is visible
Dec
23
Written by:
12/23/2010 1:27 PM

This blog post wont tell you how to save data when the keyboard is visible, but it will tell you how to avoid the problem.

If a user is entering something in a TextBox, and the keyboard is visible, If they click a Save button, it will not save what they are currently typing because the values they are typing have not been bound to the TextBox yet.
One way around this, is to disable the Save button whenever they are entering something in a TextBox.

First, give the control a name so you can access it programmatically.
Wire-up some event handlers for GotFocus and LostFocus:
public ExpenseDetailsPage()
{
InitializeComponent();
ApplicationBar = new ApplicationBar();
ApplicationBar.IsVisible = true;
ApplicationBar.IsMenuEnabled = true;
ApplicationBarIconButton btnSaveTrip = new ApplicationBarIconButton(new Uri("/icons/appbar.save.rest.png", UriKind.Relative));
btnSaveTrip.Text = "Save Trip";
btnSaveTrip.Click += new EventHandler(btnSaveTrip_Click);
ApplicationBar.Buttons.Add(btnSaveTrip);
txtTripDetails.GotFocus += new RoutedEventHandler(TripEditing_GotFocus);
txtTripDetails.LostFocus += new RoutedEventHandler(TripEditing_LostFocus);
}
Then implement the methods to enable and disable the Save button:
void TripEditing_LostFocus(object sender, RoutedEventArgs e)
{
var SaveButton =
ApplicationBar.Buttons.Cast<iapplicationbariconbutton>().Where(X => X.Text == "Save Trip").FirstOrDefault();
if (SaveButton != null)
{
SaveButton.IsEnabled = true;
}
}
void TripEditing_GotFocus(object sender, RoutedEventArgs e)
{
var SaveButton =
ApplicationBar.Buttons.Cast<iapplicationbariconbutton>().Where(X => X.Text == "Save Trip").FirstOrDefault();
if (SaveButton != null)
{
SaveButton.IsEnabled = false;
}
}
3 comment(s) so far...
Re: Windows Phone 7: Saving Data when Keyboard is visible
Good tip!
I have also gotten around this by calling this.Focus(); inside of the SaveButton Click EventHandler. This forces the textbox to loose focus and which updates the bound property.
Just thought I would throw that out there. The use case or story should determine which is the correct solution.
By Ian T. Lackey on
12/23/2010 9:39 PM
|
Re: Windows Phone 7: Saving Data when Keyboard is visible
@Ian - That is a good solution. Where were you 6 hours ago? Hahaha
By Michael Washington on
12/23/2010 10:16 PM
|
Re: Windows Phone 7: Saving Data when Keyboard is visible
)))
By Sergii Lutai on
12/26/2010 1:05 PM
|