【问题标题】:Textbox.Text changes itself without firing Textchanged event (not binding problem)Textbox.Text 在不触发 Textchanged 事件的情况下自行更改(不是绑定问题)
【发布时间】: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


【解决方案1】:

运行整个项目后...

(感谢您通过电子邮件完整地提供它。这使得调试变得更加容易)...

事件处理程序存在一些问题,但您的 Magic 值的实际原因是每次导航到 ConversationPage 时都会创建一个新的 ConversationPage 对象,但前一个对象(s) 未被销毁或重复使用。

如果您多次离开 ConversationPage,您实际上会为曾经创建的每个 ConversationPage 对象实例点击一次 SendButton_Click

原因是您的 SendButton 对象是一个单例,跨页面共享,因此连接到它的每个页面都有自己的点击事件。页面和静态 SendButton 对象之间的链接的存在意味着对话页面永远不会被删除(你有它在皮带上!)。

您需要删除 SendButton 处理程序以响应 OnNavigatedFrom 页面事件,如下所示:

SendButton.Click -= SendButton_Click;

这将删除当前页面的处理程序并让它优雅地死去。

【讨论】:

  • 是的,我有这段代码,但是我在 SendButton_click 事件处理程序的开头放置了一个断点(在执行处理程序的单行之前),此时 MessageBox.Text 已经更改。 ..
  • 我不知道它是否会在没有整个代码的情况下复制。错误可能在其他地方...如果没有任何反应,请告诉我,我会将完整的项目发送给您
  • @HiTech Magic: 好的,今晚你要做什么?我在欧洲^^
  • 非常感谢您的回答!它工作得很好!为了提高我的知识,我应该怎么做才能重用 ConversationPage 而不是每次都创建一个新的?还是要毁掉以前的?
  • @Arthur:要破坏,您需要处理 OnNavigatedFrom 事件并删除点击处理程序。然后页面将自己优雅地消失。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-09-30
  • 1970-01-01
相关资源
最近更新 更多