如何强制更新 ActualWidth 和 ActualHeight (silverlight)
我的 silverlight 控件上有一个网格,我以编程方式添加画布,并在画布中加载并显示图像。
我还在画布上添加了旋转。 问题是默认情况下旋转的 CenterX 和 CenterY 位于画布的左上角。 我想要的是围绕画布中心进行旋转。
为此,我尝试将旋转的 CenterX 和 CenterY 设置为图像 ActualWidth
/ 2 和 ActualHeight
/ 2,但是我发现 ActualWidth
和 ActualHeight
并不总是填充,至少不会立即填充。 我怎样才能强制他们更新?
即使在图像上使用 DownloadProgress 事件似乎也不能保证填充 ActualWidth 和 ActualHeight,使用 this.Dispatcher.BeginInvoke() 也不能保证填充 ActualWidth 和 ActualHeight...
Image imgTest = new Image();
Canvas cnvTest = new Canvas();
Uri uriImage = new Uri("myurl", UriKind.RelativeOrAbsolute);
System.Windows.Media.Imaging.BitmapImage bmpDisplay = new System.Windows.Media.Imaging.BitmapImage(uriImage);
bmpDisplay.DownloadProgress += new EventHandler<System.Windows.Media.Imaging.DownloadProgressEventArgs>(this.GetActualDimensionsAfterDownload);
imgTest.Source = bmpDisplay;
imgTest.Stretch = Stretch.Uniform;
imgTest.HorizontalAlignment = HorizontalAlignment.Center;
imgTest.VerticalAlignment = VerticalAlignment.Center;
cnvTest.Children.Add(imgTest);
this.grdLayout.Children.Add(imgTest);
this.Dispatcher.BeginInvoke(new Action(GetActualDimensions));
I a grid on my silverlight control, I am programatically adding a canvas, and in the canvas I am loading and displaying Image.
I'm also adding a rotation to the canvas. The problem is that by default the CenterX and CenterY of the rotation is the top left of the canvas. What I want is the rotation to happen around the centre of the canvas.
To do this, I've tried setting the CenterX and CenterY of the Rotation to the Images ActualWidth
/ 2 and ActualHeight
/ 2, however I've discovered that ActualWidth
and ActualHeight
are not always populated, at least not right away. How can I force them to get updated?
Even using the DownloadProgress event on the image doesn't seem to guarantee the ActualWidth and ActualHeight are populated, and neither does using this.Dispatcher.BeginInvoke()...
Image imgTest = new Image();
Canvas cnvTest = new Canvas();
Uri uriImage = new Uri("myurl", UriKind.RelativeOrAbsolute);
System.Windows.Media.Imaging.BitmapImage bmpDisplay = new System.Windows.Media.Imaging.BitmapImage(uriImage);
bmpDisplay.DownloadProgress += new EventHandler<System.Windows.Media.Imaging.DownloadProgressEventArgs>(this.GetActualDimensionsAfterDownload);
imgTest.Source = bmpDisplay;
imgTest.Stretch = Stretch.Uniform;
imgTest.HorizontalAlignment = HorizontalAlignment.Center;
imgTest.VerticalAlignment = VerticalAlignment.Center;
cnvTest.Children.Add(imgTest);
this.grdLayout.Children.Add(imgTest);
this.Dispatcher.BeginInvoke(new Action(GetActualDimensions));
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
要更新
FrameworkElement
的ActualWidth
和ActualHeight
,您必须调用UpdateLayout
。To update the
ActualWidth
andActualHeight
of aFrameworkElement
you will have to callUpdateLayout
.不幸的是,根据您的情况,调用 updateLayout 并不总是有效。
我在做类似的事情时运气更好:
但即使这样也经常失败。
Unfortunately, calling updateLayout doesn't always work either depending on your situation.
I've had better luck doing something like:
but even that fails too often.
我发现最可靠的方法是使用 DependencyPropertyDescriptor AddValueChanged 监听器 ActualWidth 和 ActualHeight 而不是 OnLayoutUpdated< /strong> 在渲染后获取元素大小
而不是使用 StackPanel、Grid 等,而是使用您所依赖的相对大小的基本元素
Most reliable method I found is to use DependencyPropertyDescriptor AddValueChanged listeners of ActualWidth and ActualHeight instead of OnLayoutUpdated to get element sizes after rendering
Instead of using StackPanel, Grid etc. use base element that you are depending on for relative sizes