[英]Textbox.Text changes itself without firing Textchanged event (not binding problem)
我是初學者,我正在為 Wndows Phone 7 創建一個應用程序。
您必須知道的第一件事是,當我第一次加載頁面時,我的代碼運行良好(從 Menu 到 ConversationPage,菜單包含對話列表)。 然后,如果我使用硬按鈕 go 回到菜單頁面,然后單擊相同的對話再次加載相同的 ConversationPage,問題開始出現。
基本上,我在應用程序欄中有一個名為 MessageBoxMessage 的文本框和一個 SendButton。
我想要的是:當我單擊 SendButton 時,它會查看 MessageBoxMessage.Text 並將該值發送到 PostToWeb function 中。
問題:當我重新加載頁面時,在框中寫一些內容並單擊發送按鈕,MessageBoxMessage.Text 神奇地更改為“”或“新消息”。
我在 MessageBoxMessage_TextChanged 事件和 SendButton_Click 事件的開頭引入了一個斷點,該值來自“blablabla”(最后一次觸發 MessageBoxMessage_TextChanged)到“”或“新消息”(當 SendButton_Click 觸發時)。
我不明白為什么......而且我還有另一個級聯問題,所以我想這是一個很大的初學者問題......(順便說一句,我已經檢查過,事件只定義一次)
對不起我的英語,我希望你能提供幫助:) 非常感謝
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");
}
}
下面是 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>
(感謝您通過 email 完整地提供它。這使得調試變得更加容易)......
事件處理程序存在一些問題,但您的Magic值的實際原因是每次您導航到 ConversationPage 時,都會創建一個新的 ConversationPage object,但之前的沒有被銷毀或重用。
如果您將 go 多次遠離 ConversationPage,您實際上會為曾經創建的每個 ConversationPage object 實例點擊一次 SendButton_Click。
原因是您的 SendButton object 是一個 singleton,跨頁面共享,因此連接到它的每個頁面都有自己的點擊事件。 該頁面與 static SendButton object 之間存在該鏈接意味着對話頁面永遠不會被刪除(您將它放在皮帶上。)。
您需要刪除 SendButton 處理程序以響應OnNavigatedFrom
頁面事件,如下所示:
SendButton.Click -= SendButton_Click;
這將刪除當前頁面的處理程序並允許它優雅地死去。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.