C# 此函数中的wait放在何处?

C# 此函数中的wait放在何处?,c#,asynchronous,C#,Asynchronous,严重性代码说明项目文件行列警告CS1998此 异步方法缺少“await”运算符,将同步运行。 考虑使用“Acess”操作符等待非阻塞API调用, 或“wait Task.Run(…)”在后台执行CPU限制的工作 线图像分析器C:\Users\Johny\Documents\visualstudio 2015\Projects\ImageParser\ImdbSample\ItemView.xaml.cs 26 41 您没有在方法体中等待任何异步操作(通过使用await运算符),因此方法定义中不需

严重性代码说明项目文件行列警告CS1998此 异步方法缺少“await”运算符,将同步运行。 考虑使用“Acess”操作符等待非阻塞API调用, 或“wait Task.Run(…)”在后台执行CPU限制的工作 线图像分析器C:\Users\Johny\Documents\visualstudio 2015\Projects\ImageParser\ImdbSample\ItemView.xaml.cs 26 41


您没有在方法体中等待任何异步操作(通过使用
await
运算符),因此方法定义中不需要
async
关键字。只要删除它,警告就会消失


这不会改变方法的语义。正如警告消息明确指出的那样,它已经同步运行了。

BitmapImage
自动异步下载图像-您无需执行任何额外操作。删除
async
关键字,警告将消失

如果您在执行任何其他操作之前必须等待图像下载,那么下面的一些代码将向您展示如何进行操作

 protected override async void OnNavigatedTo(NavigationEventArgs e)
 {
       base.OnNavigatedTo(e);

       var content = (Content) Application.Current.Resources["NavigationParam"];

       titleName.Text = content.title;
       var uri = new Uri(content.url, UriKind.Absolute);
       imageShow.Source = new BitmapImage(uri);
 }
//创建一个任务源,我们可以稍后等待
TaskCompletionSource taskSource=新的TaskCompletionSource();
titleName.Text=content.title;
//创建一个图像
var image=new System.Windows.Media.Imaging.BitmapImage();
//订阅图像下载完成事件-根据下载是否完成,将结果设置为true或false。
image.downloadscompleted+=(发送方,参数)=>taskSource.TrySetResult(true);
image.DownloadFailed+=(发送方,参数)=>taskSource.TrySetResult(false);
//设置uri以开始下载
image.UriSource=新Uri(content.url,UriKind.Absolute);
//等待任务提取结果
bool wasdowloadsuccessful=wait taskSource.Task;

您是否使用此方法执行任何异步工作?将
async
修饰符放在方法上不会使任何东西神奇地并行运行。如果这是你的思路,那就什么都没有了。我正在将devianart.com上的图像解析到我的windows phone应用程序中。我在后台下载图像这与
async wait
无关。我建议您阅读,我想这就是您想要的。您正在编写的方法不能是异步的:因为您需要更改标题名和imageShow。源代码,此代码必须在页面呈现之前执行,因此必须进入页面生命周期。异步方法用于不能停止页面生命周期的长时间活动(发送电子邮件或其他不影响页面结构的操作)
// create a task source that we can await on later
TaskCompletionSource<bool> taskSource = new TaskCompletionSource<bool>();

titleName.Text = content.title;

// create an image
var image = new System.Windows.Media.Imaging.BitmapImage();

// subscribe to the images download complete events - set results to true or false depending on if the download finish ok.
image.DownloadCompleted += (sender, args) => taskSource.TrySetResult(true);
                image.DownloadFailed += (sender, args) => taskSource.TrySetResult(false);

// set the uri to start the download
image.UriSource = new Uri(content.url, UriKind.Absolute);

// await the task to extract the result    
bool wasDownloadSuccessful = await taskSource.Task;