Textbox.Text 在不触发 Textchanged 事件的情况下自行更改(非绑定问题) [英] Textbox.Text changes itself without firing Textchanged event (not binding problem)

查看:38
本文介绍了Textbox.Text 在不触发 Textchanged 事件的情况下自行更改(非绑定问题)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是初学者,正在为 Wndows Phone 7 创建应用程序.

I am a beginner and I am creating an application for Wndows Phone 7.

您必须知道的第一件事是,当我第一次加载页面时,我的代码运行良好(从 Menu 到 ConversationPage,带有一个包含对话列表的 Menu).然后,如果我使用硬按钮返回菜单页面,然后单击相同的对话再次加载相同的 ConversationPage,问题就会开始发生.

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.

基本上,我在 applicationBar 中有一个名为 MessageBoxMessage 的文本框和一个 SendButton.

Basically, I have a textbox called MessageBoxMessage, and a SendButton in the applicationBar.

我想要的是:当我单击 SendButton 时,它会查看 MessageBoxMessage.Text 并在 PostToWeb 函数中发送该值.

What I want is: when I click the SendButton, it looks at MessageBoxMessage.Text and sends that value in the PostToWeb function.

问题:当我重新加载页面时,在框中写入一些内容并单击 SendButton,MessageBoxMessage.Text 会神奇地更改为"或新消息".

Problem: when I reload the page, write something in the box and click SendButton, the MessageBoxMessage.Text changes magically to either "" or "new message".

我在 MessageBoxMessage_TextChanged 事件和 SendButton_Click 事件开始处引入了一个断点,该值来自blablabla"(最近一次 MessageBoxMessage_TextChanged 触发)到"或新消息"(当 SendButton_Click 触发时).

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");
        }


    }

下面是 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>

推荐答案

运行整个项目后...

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

After running the whole project...

(thanks for supplying it complete via email. That made it much easier to debug)...

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

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.

如果您不止一次离开 ConversationPage,您实际上会在每次创建的 ConversationPage 对象实例中点击一次 SendButton_Click.

这样做的原因是您的 SendButton 对象是一个单例,跨页面共享,因此连接到它的每个页面都有自己的点击事件.页面和静态 SendButton 对象之间存在该链接意味着永远不会删除对话"页面(您将其束缚住了!).

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!).

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

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.

这篇关于Textbox.Text 在不触发 Textchanged 事件的情况下自行更改(非绑定问题)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆