【发布时间】:2015-08-14 10:13:50
【问题描述】:
我有一个问题困扰了我好几天,我已经尝试了所有选项,现在我将发布我自己的问题以寻求你们的一些具体帮助。
我需要在代码块的开头更新一个 TextBlock,它通过一个简单的按钮单击运行。
这是我的代码:
private void NewProject(bool blnCopy = false, string strFileName = null)
{
if (App.ApplicationBusy == false)
{
App.ApplicationBusy = true;
try
{
Action action = delegate()
{
Cursor = Cursors.Wait;
lblStatus.Text = "Opening Project...";
};
Dispatcher.Invoke(DispatcherPriority.Send, action);
if (blnCopy == false) { Project = new GSProject((App.RecentProjectCount + 1)); }
if (Project != null)
{
Projects.Add(Project);
if (blnCopy == false)
{
if (strFileName == null)
{
Project.ProjectName = string.Format("GSProject{0}", Projects.Count.ToString());
Project.ProjectDescription = string.Format("{0} - HW GS Project", Project.ProjectName);
Project.LoadResource();
}
else
{
Project.Load(strFileName);
}
}
else
{
Project = Project.Copy();
}
p_objNewPane = objDocker.AddDocument(Project.ProjectDisplayName, Project);
if (p_objNewPane != null)
{
p_objNewPane.DataContext = Project;
BindingOperations.SetBinding(p_objNewPane, ContentPane.HeaderProperty, new Binding("ProjectDisplayName") { Source = Project });
p_objNewPane.Closing += new EventHandler<PaneClosingEventArgs>(ContentPane_Closing);
}
if (Project.CalculationExists == true)
{
InitializeCalculation(true);
}
}
tabStartPage.Visibility = Visibility.Collapsed;
objDocumentTabs.SelectedIndex = 0;
}
catch (Exception ex)
{
ModernDialog.ShowMessage(string.Format("An error has occurred:{0}{0}{1}", Environment.NewLine, ex.Message), "Error", MessageBoxButton.OK, Application.Current.MainWindow);
}
finally
{
App.ApplicationBusy = false;
Cursor = Cursors.Arrow;
AppStatus = "Ready";
p_objNewPane = null;
}
}
}
在 try 块开始时,我需要更新 TextBlock (lblStatus) 以说明发生了什么。 void 本身 NewProject 位于 MainWindow 上,通过单击按钮调用。
谁能告诉我我哪里出错了?我已经尝试了无数可能的解决方案,所以如果我回复你说我已经尝试过了,请不要生气。
问候,汤姆。
【问题讨论】:
-
你没有说是什么问题,只是说你要更新一个TextBox..?
-
抱歉,问题是 TextBlock 根本没有更新。它只是说“就绪”(在加载 MainWindow 时设置)。
标签: c# wpf multithreading xaml asynchronous