本文介绍了任务加载图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我有一个名为Cameleon.png的图像,位于Assert文件夹中。 int我的MainPage_Loaded函数我写的是这样的东西 void MainPage_Loaded( object sender,RoutedEventArgs e) {任务< writeablebitmap> imtask = GetImage(); imtask.ContinueWith(t = > { WriteableBitmap writeableBmp = t.Result; ImageName.Source = writeableBmp; }); } async 任务< writeablebitmap> GetImage() { string strImageName = string .Format( ms-appx:///Assets/Cameleon.png); Uri uri = new Uri(strImageName); WriteableBitmap bitMap = await new WriteableBitmap( 1 , 1 )。FromContent(uri); // ImageName.Source = bitMap; return bitMap;我的XAML文件中的} < 画布 > Image Canvas.Left =0画布.Top =0x:Name =ImageName < / Canvas > 如果我设置ImageName.Source = bitMap;在我的任务函数中它显示图像,但如果在我的MainPage_Loaded函数中设置它不显示。 我做错了什么? 你的帮助非常适合。 祝你好运 Agha Khan 解决方案 这是非常不寻常的行为,但有一个解决方案。 ContinueWith调用需要一个额外的参数。 imtask.ContinueWith(t => { if(t.IsFaulted == false) { WriteableBitmap writeableBmp = t.Result; myImage.Source = writeableBmp; } },TaskScheduler.FromCurrentSynchronizationContext()); 现在它正常工作。 I have an image called Cameleon.png sitting in Assert Folder.int my MainPage_Loaded function I write something like thisvoid MainPage_Loaded(object sender, RoutedEventArgs e){ Task<writeablebitmap> imtask = GetImage(); imtask.ContinueWith(t => { WriteableBitmap writeableBmp = t.Result; ImageName.Source = writeableBmp; });}async Task<writeablebitmap> GetImage(){ string strImageName = string.Format("ms-appx:///Assets/Cameleon.png"); Uri uri = new Uri(strImageName); WriteableBitmap bitMap = await new WriteableBitmap(1, 1).FromContent(uri); // ImageName.Source = bitMap; return bitMap;}in my XAML file<Canvas> Image Canvas.Left="0" Canvas.Top="0" x:Name="ImageName"</Canvas>If I set ImageName.Source = bitMap; in my Task function it show the image, but if set in my MainPage_Loaded function it does NOT show.What I am doing wrong?Your help will be very much appriciated.Best regardsAgha Khan 解决方案 This is very unusual behavior, but there is a solution. There is need an extra parameter for ContinueWith call.imtask.ContinueWith(t => { if (t.IsFaulted == false) { WriteableBitmap writeableBmp = t.Result; myImage.Source = writeableBmp; } }, TaskScheduler.FromCurrentSynchronizationContext());Now it works fine. 这篇关于任务加载图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-29 00:40