C# 自定义事件无法更改文本块文本

C# 自定义事件无法更改文本块文本,c#,windows-phone-7,windows-phone,C#,Windows Phone 7,Windows Phone,我有一个类,它有一个简单的事件,但是当事件发生时,订阅的方法应该改变TextBlock.Text,从而改变事件参数,而不做任何事情。我不知道为什么会这样?这可能是一件很简单的事情,但我找不到答案 <!-- this is the event of my WordHelper class --> public delegate void WordHelperHandler(string attempt); public event WordHelperHandler WordPars

我有一个类,它有一个简单的事件,但是当事件发生时,订阅的方法应该改变TextBlock.Text,从而改变事件参数,而不做任何事情。我不知道为什么会这样?这可能是一件很简单的事情,但我找不到答案

<!-- this is the event of my WordHelper class -->
public delegate void WordHelperHandler(string attempt);
public event WordHelperHandler WordParsed;

<!-- this is excerpt from MainPage class -->
public MainPage()
    {
        InitializeComponent();
        helper = new WordHelper();
        helper.WordParsed += SetText;
        helper.Load(); //this method calls the event
    }
public void SetText(string text)
    {
        PageTitle.Text = text;
    }

公共委托无效WordHelperHandler(字符串尝试);
公共事件WordHelperHandler WordParsed;
公共主页()
{
初始化组件();
helper=新的WordHelper();
helper.WordParsed+=SetText;
helper.Load();//此方法调用事件
}
公共void SetText(字符串文本)
{
PageTitle.Text=文本;
}

听起来您的代码基本上在UI线程上做了大量工作。在完成之前,用户界面不会做出响应

相反,您应该在不同的线程中运行后台任务。然后,在事件处理程序中,需要使用返回UI线程以更新文本框:

public void SetText(string text)
{
    Action action = () => PageTitle.Text = text;
    Dispatcher.BeginInvoke(action);
}

那么,
WordParsed
事件是否由助手引发过?是的,当然,我在代码注释中编写了它,我在调试器中跟踪了代码并执行了它,但是没有发生任何事情。您是否在
SetText
方法中设置了断点?事件确实有效——但是只看到代码的一小部分,很难帮助您。(一个简短但完整的程序会更容易帮助您。)在load方法中有一个很长的循环,我发现,只有当load方法结束时,文本才会改变(我在第一次迭代后中断了循环)。那么,有没有一种方法可以强制事件在被调用时产生异常的效果呢?啊,那是完全不同的事情。是的,你需要另一条线。