Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从URL缓存图像时显示加载_C#_Wpf_Image_Loading_Mahapps.metro - Fatal编程技术网

C# 从URL缓存图像时显示加载

C# 从URL缓存图像时显示加载,c#,wpf,image,loading,mahapps.metro,C#,Wpf,Image,Loading,Mahapps.metro,我在WPF C#中有一个代码,用于从web加载图像: if (myImgURL != "") { var imgBitmap = new BitmapImage(); imgBitmap.BeginInit(); imgBitmap.UriSource = new Uri(myImgURL, UriKind.RelativeOrAbsolute); imgBitmap.CacheOption = BitmapCacheOption.OnLoad; imgB

我在WPF C#中有一个代码,用于从web加载图像:

if (myImgURL != "")
{
    var imgBitmap = new BitmapImage();
    imgBitmap.BeginInit();
    imgBitmap.UriSource = new Uri(myImgURL, UriKind.RelativeOrAbsolute);
    imgBitmap.CacheOption = BitmapCacheOption.OnLoad;
    imgBitmap.EndInit();
    myImgControl.Source = imgBitmap;
}
它工作得很好,但有时需要一段时间才能显示图像(如果互联网速度很慢的话)。如何在图像加载时显示并启用ProgressRing(来自工具箱),然后在图像显示时消失


我不知道在下载映像和完全加载映像时有任何事件触发器。

请查看BitmapSource类(BitmapImage的基类)中的以下事件:


还有一张便条。您正在从Uri创建BitmapImage并立即显示它。因此,无需设置
BitmapCacheOption.OnLoad
(仅当您从应在
EndInit
之后立即关闭的流中加载时,才需要此afaik)。因此,您可以这样缩短代码:

if (!string.IsNullOrEmpty(myImgURL))
{
    var imgBitmap = new BitmapImage(new Uri(myImgURL));
    myImgControl.Source = imgBitmap;

    if (imgBitmap.IsDownloading)
    {
        // start download animation here

        imgBitmap.DownloadCompleted += (o, e) =>
        {
            // stop download animation here
        };

        imgBitmap.DownloadFailed += (o, e) =>
        {
            // stop download animation here
        };
    }