我正在使用Windows 10 Windows.ApplicationModel.Chat API。我似乎无法弄清楚如何从 ChatMessageAttachment 对象中的 DataStreamReference 属性中读取数据。到目前为止,这是我的代码。
正如您所看到的,它会抛出一个异常,并显示消息“指定的强制转换无效”。 IBuffer 界面没有任何意义,因为所有内容都是长度和容量属性。
如果有人可以提供一些有关如何从 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;
}
}
}
答案 0 :(得分:1)
试试这个:
using (var datastream = (await dataStream.OpenReadAsync()).AsStreamForRead())
{
StreamReader reader = new StreamReader(datastream);
string result = await reader.ReadToEndAsync();
}