[英]How can I implement IRandomAccessStream in C#?
我想在C#中實現一個IRandomAccessStream
實例(它將返回實時生成的數據)。 該流實際上不需要是可寫的或可搜索的,但我想在ReadAsync
方法中返回我自己的數據(它實際上是IInputStream
一部分)。
public IAsyncOperationWithProgress<IBuffer, uint> ReadAsync(IBuffer buffer, uint count, InputStreamOptions options)
{
throw new NotImplementedException("To be done");
}
我的兩個主要問題是:
IAsyncOperationWithProgress
? 框架中是否有任何內容可以幫助解決這個問題? IBuffer
只有Length
和Capacity
屬性(具體的Buffer類不再提供)。 我發現這篇博客文章,希望這個IRandomAccessStream
實現可以成為你的起點。
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);
}
}
使用AsyncInfo.Run(Func<CancellationToken, IProgress<uint>, Task<IBuffer>>)
方法從委托創建IAsyncOperationWithProgress實例。
public IAsyncOperationWithProgress<IBuffer, uint> ReadAsync(IBuffer buffer, uint count, InputStreamOptions options) { if (buffer == null) throw new ArgumentNullException("buffer"); Func<CancellationToken, IProgress<uint>, Task<IBuffer>> taskProvider = (token, progress) => ReadBytesAsync(buffer, count, token, progress, options); return AsyncInfo.Run(taskProvider); } private async Task<IBuffer> ReadBytesAsync(IBuffer buffer, uint count, CancellationToken token, IProgress<uint> progress, InputStreamOptions options) { ... Fill the buffer here. Report the progress. return buffer; }
通常,您不需要直接訪問緩沖區數據。 但是如果您需要在c#中執行此操作,則可以使用System.Runtime.InteropServices.WindowsRuntime.WindowsRuntimeBufferExtensions類將數據復制到緩沖區或從緩沖區復制數據。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.