【问题标题】:Why am I getting "'System.Array' does not contain a definition for 'AsBuffer'" with this WinRT code?为什么我使用此 WinRT 代码得到“'System.Array' 不包含 'AsBuffer' 的定义”?
【发布时间】:2014-12-19 19:02:00
【问题描述】:

根据this,可以使用以下代码将字节数组转换为BitmapImage:

public static async Task<BitmapImage> ByteArrayToBitmapImage(this byte[] byteArray)
{
    if (byteArray != null)
    {
        using (var stream = new InMemoryRandomAccessStream())
        {
            await stream.WriteAsync(byteArray.AsBuffer());
            var image = new BitmapImage();
            stream.Seek(0);
            image.SetSource(stream);
            return image;
        }
    }
    return null;
}

但是,我得到,“'System.Array' 不包含'AsBuffer' 的定义,并且找不到接受'System.Array' 类型的第一个参数的扩展方法'AsBuffer'(你是缺少 using 指令或程序集引用?)"

是不是“var stream”赋值太模糊(隐式输入),我需要为“stream”var 指定一个特定的数据类型? System.Array 以外的东西?

也许这来自“Windows Store Apps Succinctly”是一个线索: 缓冲区/字节数组—System.Runtime.InteropServices.WindowsRuntime。 WindowsRuntimeBufferExtensions:此类中的扩展方法提供了在 .NET 字节数组和 WinRT 缓冲区内容之间移动的方法,公开为 IBuffer 实现。

...但如果是的话,那还不足以让我知道如何处理它。而不是“TMI”,而是“NEI”(信息不足)。

【问题讨论】:

    标签: c# windows-runtime bytearray bitmapimage system.array


    【解决方案1】:

    问题是编译器没有找到extension methodAsBuffer()。确保你引用了命名空间System.Runtime.InteropServices.WindowsRuntime,即

    using System.Runtime.InteropServices.WindowsRuntime;
    

    您还需要add a reference 到相应的 DLL,如果您还没有:

    命名空间:System.Runtime.InteropServices.WindowsRuntime

    程序集:System.Runtime.WindowsRuntime(在 System.Runtime.WindowsRuntime.dll 中)

    【讨论】:

    • 我尝试添加引用,但收到无法添加的错误消息,因为它已被构建系统自动引用。所以我添加了 using 语句并编译。所以这让我想知道,如果所需的参考已经存在,为什么 VS 不知道并给我一个“解决”上下文菜单项?无论如何,一切都很好,我猜 - 谢谢!
    猜你喜欢
    • 2011-06-21
    • 2013-11-15
    • 2014-11-23
    • 2015-02-05
    • 2012-09-29
    • 2016-10-22
    • 1970-01-01
    • 2015-07-24
    • 1970-01-01
    相关资源
    最近更新 更多