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:
- how do I return something that implements
IAsyncOperationWithProgress
? Is there anything built into the framework to help with this? - how do I write data into the buffer?
IBuffer
only hasLength
andCapacity
properties (the concrete Buffer class doesn't offer any more either).
Use
AsyncInfo.Run(Func<CancellationToken, IProgress<uint>, Task<IBuffer>>)
method to create IAsyncOperationWithProgress instance from a delegate.Usually you do not need to access the buffer data directly. But in case you need to do this in c# you can use System.Runtime.InteropServices.WindowsRuntime.WindowsRuntimeBufferExtensions class to copy data to/from a buffer.
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.