WinRT中读取基础数据类型
来源:互联网 发布:angular route.min.js 编辑:程序博客网 时间:2024/05/16 10:32
可以用Windows.Storage.Streams.DataWriter 和 Windows.Storage.Streams.DataReader 类
DataWriter类如下:
public sealed class DataWriter : IDataWriter, IDisposable {// Constructs a DataWriter over a growable buffer (see DetachBuffer below)public DataWriter();// Constructs a DataWriter over an output stream and a growable bufferpublic DataWriter(IOutputStream outputStream);// All WriteXxx methods append data to the buffer (growing it if necessary)public void WriteBoolean(Boolean value);public void WriteByte(Byte value);public void WriteBytes(Byte[] value);public void WriteBuffer(IBuffer buffer);public void WriteBuffer(IBuffer buffer, UInt32 start, UInt32 count);public void WriteInt16(Int16 value);public void WriteUInt16(UInt16 value);public void WriteInt32(Int32 value);public void WriteUInt32(UInt32 value);public void WriteInt64(Int64 value);public void WriteUInt64(UInt64 value);public void WriteSingle(Single value);public void WriteDouble(Double value);public void WriteGuid(Guid value);public void WriteDateTime(DateTimeOffset value);public void WriteTimeSpan(TimeSpan value);// For WriteXxx methods, indicates how bytes append to buffer (big/little endian)public ByteOrder ByteOrder { get; set; } // Default=BigEndian// Strings are encoded via UnicodeEncoding (Utf8, Utf16LE, or Utf16BE) instead of ByteOrderpublic UnicodeEncoding UnicodeEncoding { get; set; } // Default=Utf8// Returns how many bytes a string requires when encoded via UnicodeEncodingpublic UInt32 MeasureString(String value);// Appends the encoded string's bytes to the bufferpublic UInt32 WriteString(String value);// Returns the current size of the bufferpublic UInt32 UnstoredBufferLength { get; }// Writes the buffer to the underlying stream & clears the internal bufferpublic DataWriterStoreOperation StoreAsync();// Returns the buffer the DataWriter was using and associates a new empty buffer with itpublic IBuffer DetachBuffer();// Disassociates stream; stream will NOT be closed when Dispose is calledpublic IOutputStream DetachStream();// Closes stream (if not detached); does NOT call StoreAsyncpublic void Dispose();// Calls FlushAsync on underlying streampublic IAsyncOperation<Boolean> FlushAsync();}
注意:只有XxxAsync执行IO操作,WriteXxx并不执行IO,而只是把字节送进内存的Buffer,必须定期调用StoreAsync让buffer写进下层的流,如果不调用StoreAsync就调用Dispose的话,内存中的buffer不会写入下层的流,会被扔掉。因为Dispose不是异步方法,不执行IO操作。
下面是DataWriter的例子:
private async void DataWriterSample(StorageFile file) {using (var dw = new DataWriter(await file.OpenAsync(FileAccessMode.ReadWrite))) {dw.WriteBytes(new Byte[] { 1, 2, 3, 4, 5 });const String text = "Some text";// Store the string length first followed by the string so we can read it back laterUInt32 encodedStringLength = dw.MeasureString(text);dw.WriteUInt32(encodedStringLength);dw.WriteString(text);UInt32 bytesStored = await dw.StoreAsync(); // Commit buffer to stream} // Close DataWriter & underlying stream}
下面是DataReader的例子:
public sealed class DataReader : IDataReader, IDisposable {// Constructs a DataReader over an existing buffer instead of loading a buffer from a streampublic static DataReader FromBuffer(IBuffer buffer);// Constructs a DataReader over an input stream and a growable bufferpublic DataReader(IInputStream inputStream);// Reads count bytes from stream appending them to bufferpublic DataReaderLoadOperation LoadAsync(UInt32 count);// Indicates whether LoadAsync can prefetch more bytes than requested to by 'count'public InputStreamOptions InputStreamOptions { get; set; }// Returns number of bytes in buffer yet to be readpublic UInt32 UnconsumedBufferLength { get; }// All ReadXxx methods read data from buffer (throwing Exception if buffer is empty)public Boolean ReadBoolean();public Byte ReadByte();public void ReadBytes(Byte[] value);public IBuffer ReadBuffer(UInt32 length);public Int16 ReadInt16();public UInt16 ReadUInt16();public Int32 ReadInt32();public UInt32 ReadUInt32();public Int64 ReadInt64();public UInt64 ReadUInt64();public Single ReadSingle();public Double ReadDouble();public Guid ReadGuid();public DateTimeOffset ReadDateTime();public TimeSpan ReadTimeSpan();// For ReadXxx methods, indicates how bytes get read from the buffer (big/little endian)public ByteOrder ByteOrder { get; set; } // Default=BigEndian// Strings are decoded via UnicodeEncoding (Utf8, Utf16LE, or Utf16BE) instead of ByteOrderpublic UnicodeEncoding UnicodeEncoding { get; set; } // Default=Utf8// Decodes codeUnitCount bytes from the buffer to a string via UnicodeEncodingpublic String ReadString(UInt32 codeUnitCount);// Returns the buffer the DataReader was using and associates a new empty buffer with itpublic IBuffer DetachBuffer();// Disassociates stream; stream will NOT be closed when Dispose is calledpublic IInputStream DetachStream();// Closes stream (if not detached)public void Dispose();}
0 0
- WinRT中读取基础数据类型
- WinRT 基础
- WinRT
- Java中基础数据类型
- 在.NET代码中调用WinRT api
- WinRT中 压缩/解压缩,加密/解密
- WinRT中实现元素拖拉效果
- WinRT中实现回到列表顶部功能
- WinRT中 导入X509 RSA公钥时问题
- 如何在 Qt中调用 WinRt API
- Java基础数据类型IO流来读取雇员信息
- WinRT界面控件Essential Studio for WinRT中如何选择模式
- Flex基础-----之Flex中数据类型
- vc中数据类型的基础解释
- OC中封装,拆包基础数据类型
- Java:java中基础数据类型与转换.
- Flex与.Net中对二进制数据类型读取
- WinRT界面控件Essential Studio中图表控件的使用
- fedora17中hadoop2.2.0在eclipse下运行wordcount
- 流年的书签被抹上最深的颜色
- 网站内容全新改版后如何快速收录
- 《相信未来》
- 浅灰色的云层飘成完美的孤线
- WinRT中读取基础数据类型
- 用C#实现AES加密算法
- 防守对方发生的反反复复反反复复反反复复反反复复反反复复反
- java设计模式(结构型)之享元模式
- css绝对定位、相对定位和文档流的那些事
- Andriod中的Context理解与分析(1)
- 编写一个windows driver 例子
- Connect C# to MySQL 程序中对数据库备份和还原
- Android中通过来电转移实现“电话已关机”,“此号码已停机”等