在Windows应用商店应用中将内存文件上传到Skydrive?

时间:2014-02-26 17:23:36

标签: c# windows-phone-8 stream windows-runtime windows-store-apps

我有以下Windows Phone 8代码将文件上传到Skydrive。该文件是序列化为XML的SQLite对象的集合

XmlWriterSettings xmlWriterSettings = new XmlWriterSettings(); 
xmlWriterSettings.Indent = true; 
XmlSerializer xs = new XmlSerializer(typeof(ObservableCollection<MyObject>));                
using (MemoryStream ms = new MemoryStream())
{       
    using (var xmlWriter = XmlWriter.Create(ms, xmlWriterSettings))
    {
        xs.Serialize(xmlWriter, MyObject);
    }
    ms.Seek(0, SeekOrigin.Begin); 
    result = await App.LiveConnectClient.UploadAsync("me/skydrive", "sync.xml", ms, OverwriteOption.Overwrite);
}

我正在尝试将此代码移植到c#Windows应用商店应用,但我遇到了问题。在Live SDK的Windows商店版本下,UploadAsync不可用。 BackgroundUploadAsync是关闭的东西,它只接受Windows.Storage.Streams.IInputStream。我不能像上面代码中的'MemoryStream'一样直接创建IInputStream

我在网上找到的所有示例都详细介绍了如何从isolatedStorage上传文件。我不想将文件写入isolatedStorage只是为了能够上传它。

有人可以共享一些代码来上传流吗?如果做不到这一点,是否有人有办法将MemoryStream转换为Windows.Storage.Streams.IInputStream

1 个答案:

答案 0 :(得分:0)

结帐InMemoryRandomAccessStream,这是MemoryStream的WinRT中的替代品(尽管MemoryStream仍然存在)。

他们在System.Runtime.InteropServices.WindowsRuntime中为它提供了一些简洁的扩展。

using System.Runtime.InteropServices.WindowsRuntime;

XmlWriterSettings xmlWriterSettings = new XmlWriterSettings(); 
xmlWriterSettings.Indent = true; 
XmlSerializer xs = new XmlSerializer(typeof(ObservableCollection<MyObject>));                
using (InMemoryRandomAccessStream ms = new InMemoryRandomAccessStream())
{       
    using (var xmlWriter = XmlWriter.Create(ms.AsStream(), xmlWriterSettings))
    {
        xs.Serialize(xmlWriter, MyObject);
    }
    ms.Seek(0); 
    result = await App.LiveConnectClient.UploadAsync("me/skydrive", "sync.xml", ms.GetInputStreamAt(0), OverwriteOption.Overwrite);
}

应该有效。希望这有助于和Happy Coding!