【发布时间】:2012-05-25 10:37:00
【问题描述】:
我试图通过后面的代码设置 wpf 图像类的源属性,但由于未知原因它不起作用。
这就是我的代码的样子
1 BitmapImage img = new BitmapImage();
2 img.UriSource = new Uri("C:\\data\\pictures\dkn.jpg");
3 pict.Source = img;
然后我做了一些谷歌搜索,我发现我需要更改我的代码,如下所示。
1 BitmapImage img = new BitmapImage();
2 img.BeginInit();
3 img.UriSource = new Uri("C:\\data\\pictures\dkn.jpg");
4 img.EndInit();
5 pict.Source = img;
但我无法理解的是上面的第 2 行和第 4 行。在设置 urisource 属性之前和之后开始和结束 init 的意义是什么。我期望 urisource 属性的行为与正常的 clr 属性一样。
请有什么想法。
【问题讨论】:
-
参见ISupportInitialize 中的备注部分。您也可以使用带有 Uri 参数的 BitmapImage constructor 并写入
pict.Source = new BitmapImage(new Uri("C:\\data\\pictures\dkn.jpg")); -
当然还有BitmapImage中的备注部分。