WP8:Dispatcher.BeginInvoke 不会在 UI 线程上执行操作



我正在使用MonoGame将Windows Phone 7游戏移植到Windows Phone 8。我的游戏使用Texture2D。FromStream从互联网加载图片,一旦它还没有在MonoGame中实现,我就尝试建议的解决方法:在UI线程中用BitmapImage加载图片。

这是代码:

Texture2D result = null;
EventWaitHandle Wait = new AutoResetEvent(false);
Deployment.Current.Dispatcher.BeginInvoke(() =>
{
BitmapImage image = new BitmapImage();
image.SetSource(stream);
WriteableBitmap writeImage = new WriteableBitmap(image);
var index = 0;
var pixels = new byte[writeImage.PixelWidth * writeImage.PixelHeight * 4];
for (var h = 0; h < writeImage.PixelHeight; h++)
for (var w = 0; w < writeImage.PixelWidth; w++)
{
var pixel = writeImage.Pixels[index++];
var wIndex = w * 4 + h * writeImage.PixelWidth * 4;
pixels[wIndex++] = (byte)(pixel >> 16 & 0xFF);
pixels[wIndex++] = (byte)(pixel >> 8 & 0xFF);
pixels[wIndex++] = (byte)(pixel >> 0 & 0xFF);
pixels[wIndex++] = (byte)(pixel >> 24 & 0xFF);
}
result = new Texture2D(graphicsDevice, writeImage.PixelWidth, writeImage.PixelHeight);
result.SetData(pixels);
Wait.Set();
});

理论上一切都还可以。但不知怎么的,从来没有调用过操作码。我在非ui工作线程Deployment中调用BeginInvoke。现在的Dispatcher不为null,并提供ui线程调度器。游戏是由VS2012的MonoGame模板制作的WP8项目。我试图获取并保存到主线程(游戏构造函数)中的静态变量dispatcher和SynchronizationContext,以确保它们完全属于ui线程,但这没有帮助。

我也尝试过这样的代码:

SynchronizationContext uiThread = Hub.SyncContext;//set in main thread (Game constructor)
var waitHandle = new object();
lock (waitHandle)
{
uiThread.Post(_ => 
{
lock (waitHandle)
{
BitmapImage image = new BitmapImage();
image.SetSource(stream);
WriteableBitmap writeImage = new WriteableBitmap(image);
var index = 0;
var pixels = new byte[writeImage.PixelWidth * writeImage.PixelHeight * 4];
for (var h = 0; h < writeImage.PixelHeight; h++)
for (var w = 0; w < writeImage.PixelWidth; w++)
{
var pixel = writeImage.Pixels[index++];
var wIndex = w * 4 + h * writeImage.PixelWidth * 4;
pixels[wIndex++] = (byte)(pixel >> 16 & 0xFF);
pixels[wIndex++] = (byte)(pixel >> 8 & 0xFF);
pixels[wIndex++] = (byte)(pixel >> 0 & 0xFF);
pixels[wIndex++] = (byte)(pixel >> 24 & 0xFF);
}
result = new Texture2D(graphicsDevice, writeImage.PixelWidth, writeImage.PixelHeight);
result.SetData(pixels);
Monitor.Pulse(waitHandle);
}
}, null);
Monitor.Wait(waitHandle);
}

但同样没有运气:执行在等待时停止,操作代码中的断点永远不会被触发。

更新:我刚刚添加了简单的代码,只有一个日志到MonoGame项目的样本。如果我等待AutoResetEvent,我永远看不到任何日志。当我删除时。WaitOne()操作开始工作。我已将其替换为are。WaitOne(1000)并按预期开始工作(为什么?)。但在图像加载方面仍然没有运气。当我在are中等待时,似乎有什么东西阻止了主线程的运行。WaitOne()。

我使用这个库解决了问题:https://github.com/Taggersoft/ImageTools这个fork完全适用于WP8,如果您删除所有Contract。需要类似的语句,效果非常好。这是我的Texture2d。FromStream替换:

public static Texture2D TextureFromStream(GraphicsDevice graphicsDevice, Stream stream, String dbg_nm)
{           
#if WP8
byte [] b = new byte[4];
stream.Read(b, 0, 4);
stream.Seek(0, SeekOrigin.Begin);
int stream_type = getStreamType(b, 0);
Texture2D result = null;
if (stream_type == 1 || stream_type == 0)
{
var image = new ExtendedImage();
if (stream_type == 0)
new JpegDecoderNano().Decode(image, stream);
else
{
try
{
new PngDecoder().Decode(image, stream);
}
catch (System.Exception ex)
{
Debug.WriteLine("Error reading " + dbg_nm + ":" + ex.Message);
}                   
}
var Colors = new Color[image.Pixels.Length / 4];
for (var i = 0; i < image.Pixels.Length / 4; i++)
{
Color unpackedColor = new Color();
unpackedColor.R = (byte)(image.Pixels[i * 4 + 0]);
unpackedColor.G = (byte)(image.Pixels[i * 4 + 1]);
unpackedColor.B = (byte)(image.Pixels[i * 4 + 2]);
unpackedColor.A = (byte)(image.Pixels[i * 4 + 3]);
Colors[i] = unpackedColor;
}
Texture2D texture2D = new Texture2D(graphicsDevice, image.PixelWidth, image.PixelHeight);
texture2D.SetData(Colors);
result = texture2D;
}
return result;
#else
return Texture2D.FromStream(graphicsDevice,  stream);
#endif
}

游戏中的构造函数:

#if WP8
Decoders.AddDecoder<PngDecoder>();
Decoders.AddDecoder<JpegDecoder>();
#endif

这实际上是一种变通方法。但在我的情况下,这种方式比uithread中的usign MS BitmapImage要好得多。

最新更新