我在我的silverlight控件上添加了一个网格,我正在编程添加一个画布,并且在画布中我正在加载和显示Image。
我还在画布上添加了一个旋转。问题是,默认情况下,旋转的CenterX和CenterY位于画布的左上角。我想要的是围绕画布中心的旋转。
为此,我尝试将旋转的CenterX和CenterY设置为图像ActualWidth /2和ActualHeight / 2,但是我发现ActualWidth和ActualHeight并不总是被填充,至少不是立即填充。我如何才能强制它们更新?
即使在图像上使用DownloadProgress事件似乎也不能保证ActualWidth和ActualHeight是填充的,使用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)); 发布于 2009-08-10 20:11:04
要更新FrameworkElement的ActualWidth和ActualHeight,您必须调用UpdateLayout。
发布于 2010-08-06 05:14:05
不幸的是,根据您的情况,调用updateLayout也并不总是有效的。
我有更好的运气做一些事情,比如:
whateverUIElement.Dispatcher.BeginInvoke(()
{
//code that needs width/height here
}
);但即使是这样,也经常失败。
发布于 2014-06-17 18:21:18
我发现最可靠的方法是不使用OnLayoutUpdated,而使用ActualWidth和ActualHeight的DependencyPropertyDescriptor AddValueChanged侦听器来获取渲染后的元素大小
DependencyPropertyDescriptor descriptor = DependencyPropertyDescriptor.FromProperty(ActualWidthProperty, typeof(StackPanel));
if (descriptor != null)
{
descriptor.AddValueChanged(uiPanelRoot, DrawPipelines_LayoutUpdated);
}
descriptor = DependencyPropertyDescriptor.FromProperty(ActualHeightProperty, typeof(StackPanel));
if (descriptor != null)
{
descriptor.AddValueChanged(uiPanelRoot, DrawPipelines_LayoutUpdated);
}
void DrawPipelines_LayoutUpdated(object sender, EventArgs e)
{
// Point point1 = elementInstrumentSampleVial.TranslatePoint(
// new Point(11.0, 15.0), uiGridMainInner);
}不使用StackPanel、Grid等,而是使用依赖于相对大小的基本元素
https://stackoverflow.com/questions/1256916
复制相似问题