简体   繁体   中英

WP7: Windows Phone 7: EventHandle seems looping

I would like to have an EventHandle for a MouseLeftButtonDown event. When called/fired the last character of a string should be removed. My code looks like this:

 public string MyString;

 private void OnMouseDownDelete(object sender, MouseButtonEventArgs e)
 {
     int MyStringLength = MyString.Length;
     MyStringLength = MyStringLength - 1;
     MyString = MyString.Substring(0, MyStringLength);
 }

But when I run this code a MouseLeftButtonDown event will start a loop until the string becomes empty.

Who can tell me what I am missing?

Since you are using Mango (7.1), you can use the Tap Event instead of the MouseLeftButtonDown event.

However, I suspect that the event is firing multiple times which is causing the deletion of the string.

A good practice is to use the Handled property so that other control's do not try to handle the same event:

private void OnMouseDownDelete(object sender, MouseButtonEventArgs e)
{
     int MyStringLength = MyString.Length;
     MyStringLength = MyStringLength - 1;
     MyString = MyString.Substring(0, MyStringLength);

     e.Handled = true;
}

You may need to post all the code that shows how the MouseLeftButtonDown event is being added.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM