Access across threads in Silverlight, unlike Controls.invoke in WinForm
In general, the Dispather.begininvoke is used.
But I also found that the use of asyncoperator or asynchronazationcontext can also be implemented to convert the context
The code is as follows
Page.xaml
<UserControl x:Class="SilverlightChat.Page"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="400" Height="300">
<Grid x:Name="LayoutRoot" Background="White">
<Grid.RowDefinitions>
<RowDefinition Height="80" />
<RowDefinition Height="80" />
</Grid.RowDefinitions>
<TextBlock x:Name="tblTest" Text="not loaded yet" VerticalAlignment="Center" HorizontalAlignment="Center" Grid.Row="0" />
<Button x:Name="btnTest" Content="ClickMe" Height="30" Width="50" Click="btnTest_Click" Grid.Row="1" />
</Grid>
</UserControl>
Page.xaml.cs
Using System.Windows.Controls;
Using System.Threading;
Using System.ComponentModel;
Namespace Silverlightchat
{
public partial class Page:usercontrol
{
Private SynchronizationContext CurrentContext;
Private AsyncOperation Asyncoper;
Public Page ()
{
InitializeComponent ();
Asyncoper = Asyncoperationmanager.createoperation (null);
This.currentcontext = synchronizationcontext.current;
}
private void btnTest_Click (object sender, System.Windows.RoutedEventArgs e)
{
Thread t = new Thread (new ThreadStart (Acrossthread));
T.start ();
}
private void Acrossthread ()
{
Asyncoper.post (Result =>
{
Tbltest.text = "haschanged";
}, NULL);
Currentcontext.post (Result =>
//{
Tbltest.text = "haschanged";
}, NULL);
}
}
}
To achieve the goal of accessing the UI across threads
I would like to ask the person. The next two types of visits are specific to ...