【问题标题】:Why do setting up URISource for BitmapImage object requires a Begin and End init?为什么为 BitmapImage 对象设置 URISource 需要 Begin 和 End init?
【发布时间】: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中的备注部分。

标签: c# .net wpf


【解决方案1】:

Begin and End init 指定对象支持简单的事务通知,用于属性的批量初始化 - MSDN

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-10-14
    • 2015-06-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多