none
metro c++中各种流怎么转换啊? RRS feed

答案

全部回复

  • File Access Sample C++ 版本有例子代码: http://code.msdn.microsoft.com/windowsapps/File-access-sample-d723e597  通过DataWriter 和 DataReader就可以做到:

    IBuffer^ --> IRandomAccessStream

    IBuffer^ buffer = ...;
    
    task<IRandomAccessStream^>(file->OpenAsync(FileAccessMode::ReadWrite)).then([this, buffer](IRandomAccessStream^ stream)
    {
        IOutputStream^ outputStream = stream->GetOutputStreamAt(0);			
        DataWriter^ dataWriter = ref new DataWriter(outputStream);
        dataWriter->WriteBuffer(buffer);
        ......
    });


    IRandomAccessStream --> IBuffer^

    已删除错误代码.

    modify to correct code:

    	create_task(KnownFolders::DocumentsLibrary->GetFileAsync("test.txt")).then([this](IStorageFile^ file)
    	{
    		create_task(file->OpenAsync(FileAccessMode::Read)).then([this](IRandomAccessStream^ stream)
    		{
    			IInputStream^ inputStream = stream->GetInputStreamAt(0);
    			DataReader^ dataReader = ref new DataReader(inputStream);
    			create_task(dataReader->LoadAsync((unsigned int)stream->Size)).then([this,dataReader](unsigned int result)
    			{
    				IBuffer^ buffer = dataReader->DetachBuffer();
            ...
    			});
    		});
    	});


    论坛中还有一模一样问题的帖子:


    Bob Bao [MSFT]
    MSDN Community Support | Feedback to us



    2012年5月30日 5:52
    版主
  • IRandomAccessStream --> IBuffer^这个转换是不是有错啊。

        IInputStream^ inputStream = stream->GetInputStreamAt(0);
         
    DataReader^ dataReader = ref new DataReader(readStream);
         
    IBuffer^ buffer = dataReader->DetachBuffer();

    inputStream 怎么都没用到呢?

    2012年6月17日 5:25
  • 又发现了一个问题,IBuffer^ --> IRandomAccessStream转换成功了,然后我让图片控件显示出来, 这时候,我想保持图片,保持不了。 如果不让图片显示出来,就是去掉以下代码:

          BitmapImage^ bitmapImage = ref new BitmapImage();
          bitmapImage->SetSource(randomAccessStream);this->image_Show->Source = bitmapImage;));

    就可以保存成功了。请问一下,是不是因为让bitmapImage->SetSource()。所以IRandomAccessStream就不能再用了?如果这时候,想通过IRandomAccessStream保存文件,有办法?谢谢

    task<IRandomAccessStream^>(file->OpenAsync(FileAccessMode::ReadWrite)).then([this, buffer](IRandomAccessStream^ stream)
    {
       
    IOutputStream^ outputStream = stream->GetOutputStreamAt(0);
       
    DataWriter^ dataWriter = ref new DataWriter(outputStream);
        dataWriter
    ->WriteBuffer(buffer);

        task<unsigned int>(datawriter->StoreAsync()).then([this, randomAccessStream,_file,datawriter](unsigned int i){
           datawriter->FlushAsync();
          BitmapImage^ bitmapImage = ref new BitmapImage();
          bitmapImage->SetSource(randomAccessStream);
          this->image_Show->Source = bitmapImage;));

    //用DataReader 把 IRandomAccessStream读取出来,然后保存。

    DataReader^ dataReader = ref new DataReader(randomAccessStream);

    .......
       
    ......
    });

    2012年6月19日 4:41