Playing with some of Windows Phone I found the problem, which was quite often reported in case control TextBox. Error is ridiculous – emulator event is raised twice. This error in my project appeared in the controls TextBlock and handling click.
The project needed to take control, which will contain CheckBox and two text boxes – TextBlock. Each click on TextBlock would change the state of the control CheckBox the opposite. There’s nothing difficult, but you can imagine my surprise, when I started the program on the emulator, and there was no change after clicking. The moment of debuggerem resulted in finding a diagnosis. The event is called twice.
And here the question arises. Does this problem occurs on phones? That, unfortunately I am unable to check, due to lack of phone. Rummaging in the network or the information not found. For this reason I decided to prepare a solution, which will 100% also work on phones.
The network you can find suggestions, in which the authors modify the control skin. It seems to me, that this is not the best solution because it partially disclaim the full functionality of the product. To me it is better to save time, clicks and skipping event is dispatched, if it occurred too soon after previous. For this purpose we need a local variable that stores the time of last click – in the example is called a clickTime and a suitably programmed event. In the example, the time between clicks was set at 500 milliseconds. In the case of the second event before the expiry of the time assumed it is ignored.
The whole looks like this:
private DateTime clickTime; private void tb_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e) { TimeSpan timeFromLastClick = DateTime.Now - this.clickTime; if (timeFromLastClick.TotalMilliseconds > 500) { // things to do, e.g. CheckBox status change this.clickTime = DateTime.Now; } else { e.Handled = true; } }
Leave A Comment