加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Windows > 正文

windows-phone-8 – 我可以用什么来替换Windows Phone 8中的InMe

发布时间:2020-12-14 05:42:05 所属栏目:Windows 来源:网络整理
导读:我知道WinRT中有两个用于IRandomAccessStream接口的类, FileRandomAccessStream和InMemoryRandomAccessStream. 现在我将我的一个应用程序移植到wp8,我的一些方法需要IRandomAccessStream作为返回类型,但我找不到InMemoryRandomAccessStream. 如何在没有Windo
我知道WinRT中有两个用于IRandomAccessStream接口的类,
FileRandomAccessStream和InMemoryRandomAccessStream.

现在我将我的一个应用程序移植到wp8,我的一些方法需要IRandomAccessStream作为返回类型,但我找不到InMemoryRandomAccessStream.

如何在没有Windows Phone 8中的InMemoryRandomAccessStream的情况下创建内存流并从中获取IRandomAccessStream?

解决方法

遇到了同样的事情.我最终使用了随机访问的临时文件流.

这是从一起克隆起来的

windows.storage.applicationdata.temporaryfolder实际上没有实现,所以我在localfolder中创建了一个文件(请确保删除它,因为你想要一些临时的东西?).

windows.storage.streams.inmemoryrandomaccessstream

这里是稍微采用的示例代码:

private async void TransferData()//object sender,RoutedEventArgs e)
    {
        Windows.Storage.StorageFolder temporaryFolder = ApplicationData.Current.LocalFolder;

        // Write data to a file
        StorageFile sampleFile = await temporaryFolder.CreateFileAsync("tempStream.txt",Windows.Storage.CreationCollisionOption.ReplaceExisting);
        IRandomAccessStream acccessStream = await sampleFile.OpenAsync(FileAccessMode.ReadWrite);

        // Initialize the in-memory stream where data will be stored.
        using (var stream = acccessStream)
        {
            // Create the data writer object backed by the in-memory stream.
            using (var dataWriter = new Windows.Storage.Streams.DataWriter(stream))
            {
                dataWriter.UnicodeEncoding = Windows.Storage.Streams.UnicodeEncoding.Utf8;
                dataWriter.ByteOrder = Windows.Storage.Streams.ByteOrder.LittleEndian;

                // Parse the input stream and write each element separately.
                string[] inputElements = "this;really;works".Split(';');
                foreach (string inputElement in inputElements)
                {
                    uint inputElementSize = dataWriter.MeasureString(inputElement);
                    dataWriter.WriteUInt32(inputElementSize);
                    dataWriter.WriteString(inputElement);
                    System.Diagnostics.Debug.WriteLine("Wrote: " + inputElement);
                }

                // Send the contents of the writer to the backing stream.
                await dataWriter.StoreAsync();

                // For the in-memory stream implementation we are using,the flushAsync call 
                // is superfluous,but other types of streams may require it.
                await dataWriter.FlushAsync();

                // In order to prolong the lifetime of the stream,detach it from the 
                // DataWriter so that it will not be closed when Dispose() is called on 
                // dataWriter. Were we to fail to detach the stream,the call to 
                // dataWriter.Dispose() would close the underlying stream,preventing 
                // its subsequent use by the DataReader below.
                dataWriter.DetachStream();
            }

            // Create the input stream at position 0 so that the stream can be read 
            // from the beginning.
            using (var inputStream = stream.GetInputStreamAt(0))
            {
                using (var dataReader = new Windows.Storage.Streams.DataReader(inputStream))
                {
                    // The encoding and byte order need to match the settings of the writer 
                    // we previously used.
                    dataReader.UnicodeEncoding = Windows.Storage.Streams.UnicodeEncoding.Utf8;
                    dataReader.ByteOrder = Windows.Storage.Streams.ByteOrder.LittleEndian;

                    // Once we have written the contents successfully we load the stream.
                    await dataReader.LoadAsync((uint)stream.Size);

                    var receivedStrings = "";

                    // Keep reading until we consume the complete stream.
                    while (dataReader.UnconsumedBufferLength > 0)
                    {
                        // Note that the call to readString requires a length of "code units" 
                        // to read. This is the reason each string is preceded by its length 
                        // when "on the wire".
                        uint bytesToRead = dataReader.ReadUInt32();
                        receivedStrings += dataReader.ReadString(bytesToRead) + "n";
                    }

                    // Populate the ElementsRead text block with the items we read 
                    // from the stream.
                    System.Diagnostics.Debug.WriteLine("Read: " + receivedStrings);

                }
            }
        }
    }

确保使用windows.storage命名空间.

关键是这一行:

IRandomAccessStream acccessStream = await sampleFile.OpenAsync(FileAccessMode.ReadWrite);

我不确定速度的含义是什么.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读