run method explained español ejemplos create await async c# windows-phone-8.1 async-await imagefilter

c# - method - Windows Phone 8.1 BitmapDecoder async/await no recorre toda la función



create async method c# (1)

Este es un seguimiento de una respuesta a una pregunta que había publicado anteriormente aquí .

Puse la respuesta sugerida en una función asíncrona:

public static async Task<int[]> bitmapToIntArray(Image bitmapImage) { string fileName = "/Images/flowers.jpg"; StorageFile file = await ApplicationData.Current.LocalFolder.GetFileAsync(fileName); IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.Read); BitmapDecoder decoder = await BitmapDecoder.CreateAsync(stream); var pixelData = await decoder.GetPixelDataAsync(); var pixels = pixelData.DetachPixelData(); var width = decoder.OrientedPixelWidth; var height = decoder.OrientedPixelHeight; int[] colors = new int[width * height]; for (var i = 0; i < height; i++) { for (var j = 0; j < width; j++) { byte r = pixels[(i * height + j) * 4 + 0]; //red byte g = pixels[(i * height + j) * 4 + 1]; //green byte b = pixels[(i * height + j) * 4 + 2]; //blue (rgba) colors[i * height + j] = r; } } return colors; }

Se está llamando desde la función principal a continuación:

public void ApplyImageFilter(Image userImage, int imageWidth, int imageHeight) { ... int[] src = PixelUtils.bitmapToIntArray(userImage).Result; ... ; }

Sin embargo, cuando paso a la línea de arriba, lo que sucede es que solo la segunda línea de la función bitmapToIntArray:

StorageFile file = await ApplicationData.Current.LocalFolder.GetFileAsync(fileName);

se espera hasta que finalice, y luego salta de vuelta al ApplyImageFilter y pasa al resto de esa función (y da un error al final). No va a ninguna línea después de la primera espera en la función bitmapToIntArray. He comparado async / await con un proyecto anterior que hice con éxito y parece que seguí el mismo procedimiento en ambas ocasiones. También lee en la funcionalidad async / await y jugaste un poco con el código pero no tuve suerte. No sé qué más puedo probar, por lo que cualquier sugerencia será muy apreciada.


Está bloqueando el hilo de interfaz de usuario llamando a Result .

Una vez que vayas asincrónico, debes sincronizar todo el camino.

public async Task ApplyImageFilter(Image userImage, int imageWidth, int imageHeight) { ... int[] src = await PixelUtils.bitmapToIntArray(userImage); ... }

Para obtener más información sobre async-await , lea los artículos en mi curaduría .