Windows Phone 8的一夫一妻制:关闭后的黑色纹理

本文关键字:黑色 纹理 Phone 一夫一妻制 Windows | 更新日期: 2023-09-27 17:54:25

我在我的一夫一妻制(3.1)windows phone 8应用程序中遇到了一个奇怪的问题。当应用程序被停用,然后激活所有纹理变成黑色。这也发生在锁屏后(UserIdleDetectionMode被启用)。

我已经检查了GraphicsDevice。IsDisposed GraphicsDevice。IsContentLost GraphicsDevice。资源丢失,但一切正常。我已经在激活和未遮蔽事件上实现了所有纹理的重新加载,但完全纹理重新加载需要太多时间。与此同时,在Marketplace上,我发现单游戏应用很容易处理关闭-激活。此外,在xna上编写的windows phone 7应用程序恢复得非常快。我对一夫一妻制做错了什么?我的应用是基于一夫一妻制WP8模板。

更新:

刚刚发现通过Content.Load(…)加载的所有纹理都恢复得非常快。但是我所有的纹理都是手工编写的:我从TileContainer加载文件,解包,用ImageTools读取其数据,创建Texture2D并使用加载的数据设置其像素。Jpeg文件也被渲染到RenderTarget2D作为BGR565来消耗空间。此外,我广泛使用RenderTarget2D来渲染带有阴影的文本标签,精灵运行时组合等等。看起来Monogame不想恢复非通过content。load加载的图像。继续调查…

Windows Phone 8的一夫一妻制:关闭后的黑色纹理

我刚收到Tom Spillman在Monogame论坛和内容上的回复。加载材料恢复正常,其他数据需要由程序重新初始化。你能做的是连接到GraphicsDevice。当重置发生时,deviceresset 事件获得通知。

根据monogame开发者的说法,丢失纹理是正常情况。我在GraphicsDevice中重载了完整的纹理。DeviceReset事件。为了使其快速工作,我实现了从xnb未压缩文件加载。只要这种格式中有像素值,它就非常简单。这是唯一的解决办法。

下面是如何读取未压缩的xnb:

private static Texture2D TextureFromUncompressedXnbStream(GraphicsDevice graphicsDevice, Stream stream)
{
    BinaryReader xnbReader = new BinaryReader(stream);
    byte cx = xnbReader.ReadByte();
    byte cn = xnbReader.ReadByte();
    byte cb = xnbReader.ReadByte();
    byte platform = xnbReader.ReadByte();
    if (cx != 'X' || cn != 'N' || cb != 'B')
        return null;
    byte version = xnbReader.ReadByte();
    byte flags = xnbReader.ReadByte();
    bool compressed = (flags & 0x80) != 0;
    if (version != 5 && version != 4)
        return null;
    int xnbLength = xnbReader.ReadInt32();
    xnbReader.ReadBytes(0x9D);//skipping processor string
    SurfaceFormat surfaceFormat = (SurfaceFormat)xnbReader.ReadInt32();
    int width = (xnbReader.ReadInt32());
    int height = (xnbReader.ReadInt32());
    int levelCount = (xnbReader.ReadInt32());
    int levelCountOutput = levelCount;
    Texture2D texture = texture = new Texture2D(graphicsDevice, width, height, false, SurfaceFormat.Color);
    for (int level = 0; level < levelCount; level++)
    {
        int levelDataSizeInBytes = (xnbReader.ReadInt32());
        byte[] levelData = xnbReader.ReadBytes(levelDataSizeInBytes);
        if (level >= levelCountOutput)
            continue;
        texture.SetData(level, null, levelData, 0, levelData.Length);
    }
    xnbReader.Dispose();
    return texture;
}