22. okt 2012 16:58

I am creating a Windows Store app in which a search can be made based on the text in a TextBox. The search function can either be activated by clicking a button or by pressing the “Enter” key while the TextBox has focus. If the search string does not validate I present a MessageDialog with an appropriate error message.

This works well when the search function is activated by clicking the button but it fails with an “System.UnauthorizedAccessException” when the “Enter” key is used for activating the search function. The reason for this is that the “Windows.UI.Popups.MessageDialog.ShowAsync” method is called twice at the same time which again is due to the KeyDown event firing twice when the “Enter” key is pressed.

According to C# textbox keydown triggered twice in metro applications this is a confirmed bug.

There are a couple of simple work-arounds:

  • Subscribe to the KeyUp event instead of the KeyDown event (mentioned in the thread).
  • Create a condition based on e.KeyStatus.RepeatCount value (also mentioned in the thread).
  • Set the e.Handled property to ‘true’.

Abonnér på mit RSS feed.   Læs også de øvrige indlæg i denne Blog.