我有一个具有简单事件的类,但是当事件发生时,订阅的方法应该相应地将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;
}
<!-- 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;
}
答案 0 :(得分:1)
听起来你的代码基本上是在UI线程上做了很多工作。在您完成之前,这不会让UI做出响应。
相反,您应该在另一个线程中运行后台任务。然后在您的事件处理程序中,您需要使用Dispatcher
返回UI线程以更新文本框:
public void SetText(string text)
{
Action action = () => PageTitle.Text = text;
Dispatcher.BeginInvoke(action);
}