【发布时间】:2011-07-22 20:10:25
【问题描述】:
我是初学者,我正在为 Wndows Phone 7 创建一个应用程序。
您必须知道的第一件事是,当我第一次加载页面时,我的代码运行良好(从 Menu 到 ConversationPage,其中包含包含对话列表的 Menu)。然后,如果我使用硬按钮返回菜单页面,并单击相同的对话以再次加载相同的 ConversationPage,则问题开始出现。
基本上,我有一个名为 MessageBoxMessage 的文本框,以及 applicationBar 中的一个 SendButton。
我想要的是:当我单击 SendButton 时,它会查看 MessageBoxMessage.Text 并在 PostToWeb 函数中发送该值。
问题:当我重新加载页面时,在框中写一些内容并单击发送按钮,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>
【问题讨论】:
-
+1 解决了一个非常有趣的问题(以及一个非常适合初学者的 WP7 应用程序!):)
标签: c# silverlight windows-phone-7