簡體   English   中英

為什么我用這個WinRT代碼得到''System.Array'不包含'AsBuffer'的定義?

[英]Why am I getting “'System.Array' does not contain a definition for 'AsBuffer'” with this WinRT code?

根據這個 ,可以使用以下代碼將字節數組轉換為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'的定義,並且沒有擴展方法'AsBuffer'可以找到類型'System.Array'的第一個參數'(你是否錯過了使用指令或者裝配參考?)

是“var stream”賦值太模糊(隱式類型),我需要為“stream”var指定一個特定的數據類型嗎? System.Array以外的東西?

也許這一點,來自“ Windows Store Apps Succinctly ”是一個線索: 緩沖區/字節數組--System.Runtime.InteropServices.WindowsRuntime。 WindowsRuntimeBufferExtensions:此類中的擴展方法提供了在.NET字節數組和WinRT緩沖區內容之間移動的方法,這些內容作為IBuffer實現公開。

...但如果是的話,那對我來說不足以讓我知道如何處理它。 而不是“TMI”,它是“NEI”(沒有足夠的信息)。

問題是編譯器沒有找到擴展方法 AsBuffer() 確保您具有對命名空間System.Runtime.InteropServices.WindowsRuntime的引用,即

using System.Runtime.InteropServices.WindowsRuntime;

如果還沒有,還需要添加對相應DLL 的引用

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM