在c#中,我试图在应用程序中淡出一个Image。它是MainPage中的一个图像。一开始效果很好。但是,如果我导航到另一个站点,让我们说Settings.xaml,然后返回到MainPage,调度程序抛出Invalid cross-thread access.
任何人都知道,这就是我试图在BackgroundWorker的_DoWork函数中设置图像不透明度的方法:
Dispatcher.BeginInvoke(() =>
{
MyImage.Opacity = opacity;
});有趣的是,它没有破裂,但是在上面加了一个breakpoint,上面写着一切都是Invalid cross-thread access.
发布于 2013-01-12 18:05:02
尝试使用动画而不是BackgroundWorker来实现衰落逻辑。也许会有帮助。
你试过这样做吗?
另一种使用Windows工具包的方法
WP7的深度转换关键概念和API
深度WP7转换自定义转换
这是旧文章,但它们可能包含有用的信息。
发布于 2013-01-13 14:05:17
试试这个:
Dispatcher.Invoke(DispatcherPriority.ContextIdle, new Action(delegate()
{
//your code in another thread you want to invoke in UI thread
}));您应该从UI线程调用它,例如从MainWindow调用它。来自我的项目:
//Event raised on ImageSource property changed while Backgroundwoker in MainWindow class:
void binding_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
MyProject.ImageCropSize crop = MyProject.ImageCropSize.SourceCrop;
this.Dispatcher.Invoke(DispatcherPriority.ContextIdle, new Action(delegate()
{
BitmapImage bmisource = image1.Source as BitmapImage;
bmisource = null;//new BitmapImage();
GC.Collect();
BitmapImage bmi = MyProject.ImageData.ConvertFromBitmapToBitmapImage(((ImageBinding)sender).BitMap, crop);
image1.Source = bmi;
((ImageBinding)sender).Clear();
}));
}https://stackoverflow.com/questions/14295902
复制相似问题