Reputation: 45
I am a beginner and I am creating an application for Wndows Phone 7.
The first thing you must know is that my code works very well when I first load the page (from Menu to ConversationPage, with a Menu that contains a list of conversations). Then if I use the hardbutton to go back to the Menu page, and click on the same conversation to load same ConversationPage again, the problem start occuring.
Basically, I have a textbox called MessageBoxMessage, and a SendButton in the applicationBar.
What I want is: when I click the SendButton, it looks at MessageBoxMessage.Text and sends that value in the PostToWeb function.
Problem: when I reload the page, write something in the box and click SendButton, the MessageBoxMessage.Text changes magically to either "" or "new message".
I have introduce a breakpoint in the MessageBoxMessage_TextChanged event and at the beginning of SendButton_Click event and the value comes from "blablabla" (lastest time MessageBoxMessage_TextChanged fired) to "" or "new message" (when SendButton_Click fire).
I can't understand why... And I have another issue that cascade so I guess it's big beginner issue... (BTW I have checked and the event are defined only once)
Sorry for my english, I hope you'll be able to help :) Many thanks
private void MessageBoxMessage_GotFocus(object sender, RoutedEventArgs e)
{
MessageBoxMessageHasFocus = true;
if (MessageBoxMessage.Text == "new message")
{
MessageBoxMessage.Text = "";
if (hasPictureAttached == true)
{ SendButton.IsEnabled = true; }
else
{ SendButton.IsEnabled = false; }
}
else if (MessageBoxMessage.Text == "")
{
if (hasPictureAttached == true)
{ SendButton.IsEnabled = true; }
else
{ SendButton.IsEnabled = false; }
}
else
{
SendButton.IsEnabled = true;
}
}
private void MessageBoxMessage_LostFocus(object sender, RoutedEventArgs e)
{
MessageBoxMessageHasFocus = false;
if (MessageBoxMessage.Text == "")
{
MessageBoxMessage.Text = "new message";
if (hasPictureAttached == true)
{ SendButton.IsEnabled = true; }
else
{ SendButton.IsEnabled = false; }
}
else if (MessageBoxMessage.Text == "new message")
{
if (hasPictureAttached == true)
{ SendButton.IsEnabled = true; }
else
{ SendButton.IsEnabled = false; }
}
else
{
SendButton.IsEnabled = true;
}
}
int MessageBoxMessageTextChangedCounter = 0;
private void MessageBoxMessage_TextChanged(object sender, TextChangedEventArgs e)
{
if (MessageBoxMessageTextChangedCounter == 0)
{
if ((MessageBoxMessage.Text != "" && MessageBoxMessage.Text != "new message") || hasPictureAttached == true)
{
SendButton.IsEnabled = true;
}
else { SendButton.IsEnabled = false; }
MessageBoxMessageTextChangedCounter = 1;
return;
}
else
{
MessageBoxMessageTextChangedCounter = 0;
}
if (MessageBoxMessage.Text != "" && MessageBoxMessage.Text != "new message")
{
MessageString = MessageBoxMessage.Text;
}
}
private void SendButton_Click(object sender, EventArgs e)
{
if (MessageBoxMessage.Text == "new message" && hasPictureAttached == true)
{ MessageBoxMessage.Text = "";}
SendButton.IsEnabled = false;
if (hasPictureAttached == true)
{
//MessageString = MessageBoxMessage.Text;
GetPictureUrl();
hasPictureAttached = false;
}
else
{
//MessageString = MessageBoxMessage.Text;
POSTmessage();
}
if (MessageBoxMessageHasFocus == true)
{
MessageBoxMessage.Text = "";
MessageBoxMessage.SetValue(TextBox.TextProperty, "");
}
else
{
MessageBoxMessage.Text = "new message";
MessageBoxMessage.SetValue(TextBox.TextProperty, "new message");
}
}
Below is the XAML
<TextBox x:Name="MessageBoxMessage" Margin="-12,0,-12,12" TextWrapping="Wrap" Foreground="Gray" TextChanged="MessageBoxMessage_TextChanged" LostFocus="MessageBoxMessage_LostFocus" GotFocus="MessageBoxMessage_GotFocus">
<TextBox.InputScope>
<InputScope>
<InputScopeName NameValue="Chat" />
</InputScope>
</TextBox.InputScope>
</TextBox>
Upvotes: 2
Views: 1123
Reputation: 93561
(thanks for supplying it complete via email. That made it much easier to debug)...
There were a couple of issues with event handlers, but the actual cause of your Magic values is that each time you are navigating to the ConversationPage a new ConversationPage object is being created, but the previous one(s) has not been destroyed or reused.
If you go away from the ConversationPage more than once you will actually hit your SendButton_Click once for every instance of the ConversationPage object ever created.
The reason for that is your SendButton object is a singleton, shared across pages, so each page that connects to it gets its own click event. The existence of that link between the page and the static SendButton object means the Conversation page is never deleted (you have it on a leash!).
You need to remove the SendButton handler in response the the OnNavigatedFrom
page event like this:
SendButton.Click -= SendButton_Click;
That will remove the handler for the current page and allow it to die a graceful death.
Upvotes: 1