我有一個<Border>
裏面我有一個<Image>
和一個<Textblock>
,我試圖改變Mouseleftbutton下的文本塊文本,因爲在這個點擊事件中有一些更冗長的操作,文本文本在此操作完成之前,塊不會更改。我也試過Dispatcher.BeginInvoke()
,但沒有成功。點擊更改文本塊文本
這裏是我的代碼:
<Border x:Name="btnReadMe" Grid.Row="0" Style="{StaticResource BorderStyle1}" MouseLeftButtonDown="btnReadMe_MouseLeftButtonDown" MouseLeftButtonUp="btnReadMe_MouseLeftButtonUp" >
<Border.Background>
<LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1">
<GradientStop Color="#46c746" Offset="0"/>
<GradientStop Color="#129312" Offset="1"/>
</LinearGradientBrush>
</Border.Background>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="3*"/>
<ColumnDefinition Width="6*"/>
</Grid.ColumnDefinitions>
<Image Grid.Column="0" Source="/CareFamily.AtHome;component/Resources/read_message-read_it.png" Margin="5,15" >
<Image.RenderTransform>
<ScaleTransform ScaleX="1" ScaleY="1"/>
</Image.RenderTransform>
</Image>
<StackPanel Grid.Column="1" Margin="0,10,0,10" VerticalAlignment="Center">
<TextBlock Name="tbReadToMe" Text="Read to Me" Style="{StaticResource TextBlockStyle1}" Margin="0,0,0,0" />
</StackPanel>
</Grid>
</Border>
SpVoice voice;
private void btnReadMe_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if (voice == null)
voice = new SpVoice();
string readMessageState = tbReadToMe.Text;
switch (readMessageState)
{
case "Read to Me":
{
tbReadToMe.Text = "Pause";
break;
}
case "Pause":
{
tbReadToMe.Text = "Resume";
voice.Pause();
break;
}
case "Resume":
{
tbReadToMe.Text = "Pause";
voice.Resume();
break;
}
default:
{
tbReadToMe.Text = "Read to Me";
break;
}
}
if (!string.IsNullOrEmpty(Msg.Subject))
{
voice.Speak(Msg.Subject, SpeechVoiceSpeakFlags.SVSFDefault);
}
if (!string.IsNullOrEmpty(Msg.Body))
{
voice.Speak(Msg.Body, SpeechVoiceSpeakFlags.SVSFDefault); // Length operation
}
}
你調用冗長的操作在一個單獨的線程?如果沒有,它將在UI線程上運行,調用Dispatcher上的文本更改將不會執行任何操作,因爲它正在等待冗長的操作完成。看到一些輔助操作將會有所幫助。另外,你可能想看看BackgroundWorker類和它的ProgressChanged事件。 –