Silverlight - 关于UI线程 / CompositionTarget.Rendering 事件

本文探讨了在Silverlight中如何使用BackgroundWorker、Dispatcher及DispatcherTimer等技术手段从非UI线程更新UI,并介绍了CompositionTarget.Rendering事件的应用,指出Silverlight与WPF在UI线程上的区别。

Pete发了一篇关于Silverlight 线程的blog

1. 使用BackgroundWorker

2. 使用Dispatcher 从非UI线程更新UI

2.1 最简单的调用Dispatcher的方法:使用Deployment .Current.Dispatcher

3. 使用DispatcherTimer

关于 CompositionTarget.Rendering 事件:Occurs just before the objects in the composition tree are rendered.

Silverlight与WPF在UI上的实现的不同:Silverlight没有单独的Rendering线程,所有rendering都是在程序的主线程UI Thread上运行。

应用:利用实现Composition­Target.Rendering事件实现 Just-in-time显示效果

private void SourceImage_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { if (sender is Image image && image.Source != null) { // 获取鼠标在屏幕上的绝对位置 Point mouseScreenPos = e.GetPosition(Application.Current.MainWindow); mouseScreenPos = Application.Current.MainWindow.PointToScreen(mouseScreenPos); // 创建预览图像 var previewImage = new Image { Source = image.Source, Stretch = Stretch.Uniform, Opacity = 0.6, Width = image.ActualWidth, Height = image.ActualHeight }; // 创建预览窗口 var previewWindow = new Window { Content = previewImage, WindowStyle = WindowStyle.None, AllowsTransparency = true, Background = Brushes.Transparent, ShowInTaskbar = false, Topmost = true, SizeToContent = SizeToContent.WidthAndHeight, ShowActivated = false, Opacity = 0.6 }; bool isWindowReady = false; previewWindow.Loaded += (s, args) => { previewWindow.UpdateLayout(); // 强制布局计算 UpdateWindowPosition(previewWindow, mouseScreenPos); isWindowReady = true; }; previewWindow.Show(); // 使用 CompositionTarget.Rendering 进行平滑更新 CompositionTarget.Rendering += (s, args) => { if (!isWindowReady) return; Win32.POINT win32Point; Win32.GetCursorPos(out win32Point); Point currentMousePos = new Point(win32Point.X, win32Point.Y); UpdateWindowPosition(previewWindow, currentMousePos); }; try { DragDrop.DoDragDrop( image, new DataObject(DataFormats.Bitmap, image.Source), DragDropEffects.Copy ); } finally { previewWindow.Close(); } } } private void UpdateWindowPosition(Window window, Point mousePosition) { if (window.IsLoaded) { // 获取DPI缩放比例 var source = PresentationSource.FromVisual(window); var dpiScale = source?.CompositionTarget.TransformToDevice ?? Matrix.Identity; var dpiFactor = 1 / dpiScale.M11; // 计算中心点(考虑DPI缩放) var offsetX = (window.ActualWidth / 2) * dpiFactor; var offsetY = (window.ActualHeight / 2) * dpiFactor; window.Left = mousePosition.X - offsetX; window.Top = mousePosition.Y - offsetY; } } // 添加 Win32 API 辅助类来获取准确的鼠标位置 public static class Win32 { [StructLayout(LayoutKind.Sequential)] public struct POINT { public int X; public int Y; public POINT(int x, int y) { X = x; Y = y; } public static implicit operator Point(POINT p) { return new Point(p.X, p.Y); } } [DllImport("user32.dll")] public static extern bool GetCursorPos(out POINT lpPoint); }鼠标没在虚影中间,
09-03
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值