Se in una finestra WPF voglio che un controllo sia aggiornato da un altro thread come devo fare?
Ho provato così: Dovrebbe essere che il testo del textblock si aggiorna ogni decimo di secondo aggiungendo un nuovo carattere, invece lo vedo solo quando il testo è completo, come se il meccanismo usato bloccasse il thread principale.
<UserControl x:Class="UsiLabManager.Control.MainControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="585" d:DesignWidth="915">
<Grid>
<Image Height="150" HorizontalAlignment="Left" Margin="141,85,0,0" Name="image1" Stretch="Fill" VerticalAlignment="Top" Width="645" Source="/UsiLabManager;component/Images/Logo%20USI.jpg" />
<TextBlock Height="174" HorizontalAlignment="Left" Margin="58,344,0,0" Name="txtMain" Text="." VerticalAlignment="Top" Width="800" FontFamily="Arial" FontSize="36" TextAlignment="Center" />
</Grid>
</UserControl>
public partial class MainControl : UserControl
{
private delegate void TBdelegate();
public MainControl()
{
InitializeComponent();
//DoubleAnimation da = new DoubleAnimation(360, 0, new Duration(TimeSpan.FromSeconds(3)));
//TranslateTransform tl = new TranslateTransform(100,0);
//image1.RenderTransform = tl;
//image1.RenderTransformOrigin = new Point(1, 0.5);
//da.RepeatBehavior = new RepeatBehavior(1);
//tl.BeginAnimation(TranslateTransform.XProperty, da);
Dispatcher.BeginInvoke(
DispatcherPriority.Normal,
new TBdelegate(SetMainText));
}
private void SetMainText()
{
string MainText = "Trasmissione dati integrata";
//txtMain.Text = MainText;
txtMain.Text = string.Empty;
foreach (char c in MainText)
{
txtMain.Text += c;
System.Threading.Thread.Sleep(100);
}
}
}