首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何强制ActualWidth和ActualHeight更新(silverlight)

如何强制ActualWidth和ActualHeight更新(silverlight)
EN

Stack Overflow用户
提问于 2009-08-10 20:01:18
回答 3查看 8.4K关注 0票数 9

我在我的silverlight控件上添加了一个网格,我正在编程添加一个画布,并且在画布中我正在加载和显示Image。

我还在画布上添加了一个旋转。问题是,默认情况下,旋转的CenterX和CenterY位于画布的左上角。我想要的是围绕画布中心的旋转。

为此,我尝试将旋转的CenterX和CenterY设置为图像ActualWidth /2和ActualHeight / 2,但是我发现ActualWidthActualHeight并不总是被填充,至少不是立即填充。我如何才能强制它们更新?

即使在图像上使用DownloadProgress事件似乎也不能保证ActualWidth和ActualHeight是填充的,使用this.Dispatcher.BeginInvoke()也不能...

代码语言:javascript
复制
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)); 
EN

回答 3

Stack Overflow用户

发布于 2009-08-10 20:11:04

要更新FrameworkElementActualWidthActualHeight,您必须调用UpdateLayout

票数 14
EN

Stack Overflow用户

发布于 2010-08-06 05:14:05

不幸的是,根据您的情况,调用updateLayout也并不总是有效的。

我有更好的运气做一些事情,比如:

代码语言:javascript
复制
whateverUIElement.Dispatcher.BeginInvoke(()
  {
   //code that needs width/height here
  }
);

但即使是这样,也经常失败。

票数 3
EN

Stack Overflow用户

发布于 2014-06-17 18:21:18

我发现最可靠的方法是不使用OnLayoutUpdated,而使用ActualWidthActualHeightDependencyPropertyDescriptor AddValueChanged侦听器来获取渲染后的元素大小

代码语言:javascript
复制
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等,而是使用依赖于相对大小的基本元素

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1256916

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档