本文介绍了我如何在C#中实现IRandomAccessStream?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现在C#中的 IRandomAccessStream 的实例(它会被返回实时产生的数据)。流实际上并不需要是可写的或可搜索的,但我想在 ReadAsync 方法返回我自己的数据(实际上是 IInputStream的一部分

 公共IAsyncOperationWithProgress< IBuffer,UINT> ReadAsync(IBuffer缓冲,UINT计数,InputStreamOptions选项)
{
抛出新NotImplementedException(要做就做);
}



我的两个主要问题是:




  1. 我怎么回事情,实现 IAsyncOperationWithProgress ?有内置的框架,任何事情来帮助这个?

  2. 我如何将数据写入缓冲区? IBuffer 只有长度容量属性(具体缓冲区类不提供任何更多其一)

解决方案



我发现这个博客文章,希望这实现 IRandomAccessStream 可以是一个起点,为你。

 类MemoryRandomAccessStream:IRandomAccessStream 
{
专用流m_InternalStream;

公共MemoryRandomAccessStream(流流)
{
this.m_InternalStream =流;
}

公共MemoryRandomAccessStream(字节[]字节)
{
this.m_InternalStream =新的MemoryStream(字节);
}

公共IInputStream GetInputStreamAt(ULONG位置)
{
this.m_InternalStream.Seek((长)的位置,SeekOrigin.Begin);

返回this.m_InternalStream.AsInputStream();
}

公共IOutputStream GetOutputStreamAt(ULONG位置)
{
this.m_InternalStream.Seek((长)的位置,SeekOrigin.Begin);

返回this.m_InternalStream.AsOutputStream();
}

公共ULONG尺寸
{
{返回(ULONG)this.m_InternalStream.Length; }
集合{this.m_InternalStream.SetLength((长)值); }
}

公共BOOL的CanRead
{
获得{返回true; }
}

公共BOOL CanWrite
{
获得{返回true; }
}

公共IRandomAccessStream CloneStream()
{
抛出新NotSupportedException异常();
}

公共ULONG位置
{
{返回(ULONG)this.m_InternalStream.Position; }
}

公共无效SEEK(ULONG位置)
{
this.m_InternalStream.Seek((长)的位置,0);
}

公共无效的Dispose()
{
this.m_InternalStream.Dispose();
}

公共Windows.Foundation.IAsyncOperationWithProgress< IBuffer,UINT> ReadAsync(IBuffer缓冲,UINT计数,InputStreamOptions选项)
{
变种的InputStream = this.GetInputStreamAt(0);
返回inputStream.ReadAsync(缓冲区,计数,期权);
}

公共Windows.Foundation.IAsyncOperation<布尔> FlushAsync()
{
变种的OutputStream = this.GetOutputStreamAt(0);
返回outputStream.FlushAsync();
}

公共Windows.Foundation.IAsyncOperationWithProgress< UINT,UINT> WriteAsync(IBuffer缓冲)
{
变种的OutputStream = this.GetOutputStreamAt(0);
返回outputStream.WriteAsync(缓冲);
}
}


I would like to implement an instance of IRandomAccessStream in C# (it will be returning data generated in realtime). The stream does not actually need to be writable or seekable, but I want to return my own data in the ReadAsync method (which is actually part of IInputStream).

public IAsyncOperationWithProgress<IBuffer, uint> ReadAsync(IBuffer buffer, uint count, InputStreamOptions options)
{
    throw new NotImplementedException("To be done");
}

My two main questions are:

  1. how do I return something that implements IAsyncOperationWithProgress? Is there anything built into the framework to help with this?
  2. how do I write data into the buffer? IBuffer only has Length and Capacity properties (the concrete Buffer class doesn't offer any more either).
解决方案

How to Convert byte Array to IRandomAccessStream

I've found this blog article, hopefully this realization of IRandomAccessStream can be a starting point for you.

class MemoryRandomAccessStream : IRandomAccessStream
{
    private Stream m_InternalStream;

    public MemoryRandomAccessStream(Stream stream)
    {
        this.m_InternalStream = stream;
    }

    public MemoryRandomAccessStream(byte[] bytes)
    {
        this.m_InternalStream = new MemoryStream(bytes);
    }

    public IInputStream GetInputStreamAt(ulong position)
    {
        this.m_InternalStream.Seek((long)position, SeekOrigin.Begin);

        return this.m_InternalStream.AsInputStream();
    }

    public IOutputStream GetOutputStreamAt(ulong position)
    {
        this.m_InternalStream.Seek((long)position, SeekOrigin.Begin);

        return this.m_InternalStream.AsOutputStream();
    }

    public ulong Size
    {
        get { return (ulong)this.m_InternalStream.Length; }
        set { this.m_InternalStream.SetLength((long)value); }
    }

    public bool CanRead
    {
        get { return true; }
    }

    public bool CanWrite
    {
        get { return true; }
    }

    public IRandomAccessStream CloneStream()
    {
        throw new NotSupportedException();
    }

    public ulong Position
    {
        get { return (ulong)this.m_InternalStream.Position; }
    }

    public void Seek(ulong position)
    {
        this.m_InternalStream.Seek((long)position, 0);
    }

    public void Dispose()
    {
        this.m_InternalStream.Dispose();
    }

    public Windows.Foundation.IAsyncOperationWithProgress<IBuffer, uint> ReadAsync(IBuffer buffer, uint count, InputStreamOptions options)
    {
        var inputStream = this.GetInputStreamAt(0);
        return inputStream.ReadAsync(buffer, count, options);
    }

    public Windows.Foundation.IAsyncOperation<bool> FlushAsync()
    {
        var outputStream = this.GetOutputStreamAt(0);
        return outputStream.FlushAsync();
    }

    public Windows.Foundation.IAsyncOperationWithProgress<uint, uint> WriteAsync(IBuffer buffer)
    {
        var outputStream = this.GetOutputStreamAt(0);
        return outputStream.WriteAsync(buffer);
     }
}

这篇关于我如何在C#中实现IRandomAccessStream?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-12 09:51