我想從嵌入式資源加載圖像到Image
實例。問題是圖像的大小始終爲0圖像大小總是返回0
這裏是代碼片段:
Image image = new Image();
ImageSource img = new System.Windows.Media.Imaging.BitmapImage(new Uri("/Images/marker.png", UriKind.Relative));
image.SetValue(Image.SourceProperty, img);
System.Diagnostics.Debug.WriteLine("output 1 = " + image.DesiredSize.Width); // return 0
System.Diagnostics.Debug.WriteLine("output 2 = " + image.ActualWidth); // return 0
我要知道之前呈現在屏幕上的圖像的大小,因爲我需要偏移圖像取決於它的大小。
謝謝
更新: 感謝Silvermind
我回答我自己的問題與示例代碼
它沒有'DesiredSize'它是在UI渲染之前。 – Silvermind
在UI上呈現之前是否可以獲取圖像大小? –
在渲染前獲取控件大小並不是真正可行的:http://msdn.microsoft.com/en-us/library/system.windows.frameworkelement.actualwidth(v=vs.95).aspx在Silverlight中,它更好想想使用HorizontalAligment等進行自動定位,或許最好告訴我們你想要完成什麼。 – Silvermind