从 IRandomAccessStreamReference 读取
Reading from IRandomAccessStreamReference
我正在使用 windows 10 Windows.ApplicationModel.Chat API。我似乎无法弄清楚如何从 ChatMessageAttachment 对象中的 DataStreamReference 属性 读取数据。到目前为止,这是我的代码。
如您所见,它抛出异常并显示消息 "Specified cast is not valid." IBuffer 接口没有任何意义,因为只有 Length 和 容量 属性.
如果有人能给我一些关于如何阅读 DataStreamReference 属性.
的提示,我将不胜感激
var store = await ChatMessageManager.RequestStoreAsync();
var conversations = await store.GetConversationReader().ReadBatchAsync();
foreach (var conversation in conversations)
{
var messages = (await conversation.GetMessageReader().ReadBatchAsync(int.MaxValue)).ToArray();
foreach (var message in messages)
{
try
{
var dataStream = await message.Attachments.First().DataStreamReference;
var stream = await dataStream.OpenReadAsync();
var buffer = new Ass(); // Implements IBuffer
var count = (uint)stream.Size;
// Throws exception - "Specified cast is not valid."
await stream.ReadAsync(buffer, count, Windows.Storage.Streams.InputStreamOptions.ReadAhead);
}
catch (Exception ex)
{
throw;
}
}
}
试试这个:
using (var datastream = (await dataStream.OpenReadAsync()).AsStreamForRead())
{
StreamReader reader = new StreamReader(datastream);
string result = await reader.ReadToEndAsync();
}
我正在使用 windows 10 Windows.ApplicationModel.Chat API。我似乎无法弄清楚如何从 ChatMessageAttachment 对象中的 DataStreamReference 属性 读取数据。到目前为止,这是我的代码。
如您所见,它抛出异常并显示消息 "Specified cast is not valid." IBuffer 接口没有任何意义,因为只有 Length 和 容量 属性.
如果有人能给我一些关于如何阅读 DataStreamReference 属性.
的提示,我将不胜感激var store = await ChatMessageManager.RequestStoreAsync();
var conversations = await store.GetConversationReader().ReadBatchAsync();
foreach (var conversation in conversations)
{
var messages = (await conversation.GetMessageReader().ReadBatchAsync(int.MaxValue)).ToArray();
foreach (var message in messages)
{
try
{
var dataStream = await message.Attachments.First().DataStreamReference;
var stream = await dataStream.OpenReadAsync();
var buffer = new Ass(); // Implements IBuffer
var count = (uint)stream.Size;
// Throws exception - "Specified cast is not valid."
await stream.ReadAsync(buffer, count, Windows.Storage.Streams.InputStreamOptions.ReadAhead);
}
catch (Exception ex)
{
throw;
}
}
}
试试这个:
using (var datastream = (await dataStream.OpenReadAsync()).AsStreamForRead())
{
StreamReader reader = new StreamReader(datastream);
string result = await reader.ReadToEndAsync();
}