【发布时间】:2014-10-02 17:32:28
【问题描述】:
我是 WPF 的新手,我注意到您不能直接从另一个线程访问 WPF 控件。相反,您必须使用 Dispatcher。看了一些stackoverflow的文章和问题,还是不知道为什么下一个代码不起作用。
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
System.Threading.Thread thread = new System.Threading.Thread(
new System.Threading.ThreadStart(
delegate()
{
TransformGroup transform = new TransformGroup();
RotateTransform rot = new RotateTransform(45);
TranslateTransform trans = new TranslateTransform(500, 500);
transform.Children.Add(rot);
transform.Children.Add(trans);
bOne.Dispatcher.Invoke(
System.Windows.Threading.DispatcherPriority.Normal,
new Action(
delegate()
{
bOne.RenderTransform = transform;
}
));
}
));
thread.Start();
}
}
xaml 在哪里:
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Custom="http://schemas.microsoft.com/surface/2008" x:Class="Prueba.MainWindow"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Custom:SurfaceButton Name="bOne" Content="SurfaceButton" HorizontalAlignment="Left" Margin="0,0,0,0" VerticalAlignment="Top"/>
</Grid>
</Window>
尝试修改 bOne.RenderTransform 时仍然抛出异常。
【问题讨论】:
标签: dispatcher