我想在应用程序启动时预加载我的图像资源.图像应缓存在应用程序内存中.所以我可以使用
我的应用程序中预装的图像.
我的应用程序中预装的图像.
问题是,如果我加载一个内部有很多图像的特定视图,
应用程序挂起几秒钟,之后会出现视图.
该应用程序基于XAML,但图像控件的source-property是动态更改的.
我测试了一些东西,但似乎没有任何效果.
var uri = new Uri ( "pack://application:,/Vibrafit.Demo;component/Resources/myImage.jpg",UriKind.RelativeOrAbsolute ); //unit.Image1Uri; var src = new BitmapImage ( uri ); src.CacheOption = BitmapCacheOption.None; src.CreateOptions = BitmapCreateOptions.None; src.DownloadFailed += delegate { Console.WriteLine ( "Failed" ); }; src.DownloadProgress += delegate { Console.WriteLine ( "Progress" ); }; src.DownloadCompleted += delegate { Console.WriteLine ( "Completed" ); };
但图像不会加载.加载图像的唯一方法是在Image-Control中将其显示在屏幕上,并将Source-Property分配给我新创建的BitmapImage-Object.但我不想在启动时显示所有图像.
解决方法
如果要立即加载图像,则需要设置此缓存选项:
src.CacheOption = BitmapCacheOption.OnLoad;
否则,它会在您第一次访问数据时按需加载(或者,在您的情况下,每次尝试访问图像数据时,因为您选择无).
此外,您在设置缓存选项之前设置UriSource.所以尝试类似的东西(从我的头脑中,现在无法测试):
var uri = new Uri ( "pack://application:,UriKind.RelativeOrAbsolute ); //unit.Image1Uri; var src = new BitmapImage (); src.BeginInit(); src.CacheOption = BitmapCacheOption.OnLoad; src.CreateOptions = BitmapCreateOptions.None; src.DownloadFailed += delegate { Console.WriteLine ( "Failed" ); }; src.DownloadProgress += delegate { Console.WriteLine ( "Progress" ); }; src.DownloadCompleted += delegate { Console.WriteLine ( "Completed" ); }; src.UriSource = uri; src.EndInit();
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。