2015-05-05 30 views
4

要求:共享文本和圖像使用DataTransferManager成Facebook在視窗10無法在Windows 10共享使用DataTransferManager圖像

問題:無法共享圖像。

下面示出的是我所使用的代碼,

private async void DataTransferManager_DataRequested(DataTransferManager sender, DataRequestedEventArgs args) 
     { 
      DataRequestDeferral deferral = args.Request.GetDeferral(); 
      args.Request.Data.Properties.Title = "Sharing sample"; 
      args.Request.Data.SetText("Testing share in universal app"); 
      var imageUri = "http://cdn.vrworld.com/wp-content/uploads/2015/01/microsoft-announces-windows-10_ahab.1920.jpg"; 

      //var storageFile = await StorageFile.CreateStreamedFileFromUriAsync("ShareFile", new Uri(imageUri), null); 
      //List<IStorageItem> storageItems = new List<IStorageItem>(); 
      //storageItems.Add(storageFile); 
      //args.Request.Data.SetStorageItems(storageItems); 

      args.Request.Data.SetBitmap(Windows.Storage.Streams.RandomAccessStreamReference.CreateFromUri(new Uri(imageUri))); 
      deferral.Complete(); 
     } 

當我使用SetBitmap方法,只有標題和文本正在被共享。該圖像既不顯示在共享面板中,也不共享給目標應用程序。

當我使用SetStorageItems(請參閱註釋代碼)時,沒有任何項目被共享。共享面板上顯示默認的「您的想法」文字。

任何反饋表示讚賞,謝謝!

+0

您是否嘗試過分享到其他應用程序? Facebook可能不支持這些數據類型。 –

+0

@彼得托爾MSFT - 我試圖分享到微博,但也有圖像沒有出現。我面臨的另一個問題是默認郵件應用程序和Google +未顯示在共享窗格的應用程序列表中。 – Bells

回答

3

不幸的是不支持共享URI流式文件。這是我怎麼會去這樣做:

  1. 當用戶點擊分享按鈕,開始下載文件 並顯示某種進步,如果它是一個很大的文件。你當然也可以預先下載文件 。設置包含該文件的StorageFile實例 。
  2. 呼叫DataTransferManager.ShowShareUI
  3. 在你DataRequested處理程序,使用SetStorageItems共享StorageFile實例。
+0

謝謝你,將檢查這種方式並更新結果。 – Bells

+0

請檢查:http://stackoverflow.com/questions/42856996/uwp-share-feature-not-working-in-windows-10-mobile?noredirect=1#comment72822335_42856996它似乎失敗有些相關。 – SuperJMN

-3

我想你指的份額的目標在UWP 你可以參考這個網址 https://github.com/Microsoft/Windows-universal-samples/tree/master/Samples/ShareSource

此示例演示如何應用程序共享內容與其他應用程序。本示例使用Windows.ApplicationModel.DataTransfer命名空間中的類。您可能想要更詳細地查看的某些類是用於啓動共享操作的DataTransferManager類和用於打包內容的DataPackage類。由於每個共享場景通常涉及兩個應用程序(源應用程序和接收內容的目標應用程序),因此我們建議您在安裝並運行此應用程序時安裝並部署共享內容目標應用程序示例。通過這種方式,您可以看到共享如何從端到端進行工作。

此示例介紹瞭如何以各種格式共享內容,其中包括:

1.Text 引擎WEB鏈接 3.應用鏈接 4.Images 5.Files 6.Delay渲染文件 7.HTML內容 8.Custom數據

protected override bool GetShareContent(DataRequest request) 
    { 
     bool succeeded = false; 

     if (this.imageFile != null) 
     { 
      DataPackage requestData = request.Data; 
      requestData.Properties.Title = TitleInputBox.Text; 
      requestData.Properties.Description = DescriptionInputBox.Text; // The description is optional. 
      requestData.Properties.ContentSourceApplicationLink = ApplicationLink; 

      // It's recommended to use both SetBitmap and SetStorageItems for sharing a single image 
      // since the target app may only support one or the other. 

      List<IStorageItem> imageItems = new List<IStorageItem>(); 
      imageItems.Add(this.imageFile); 
      requestData.SetStorageItems(imageItems); 

      RandomAccessStreamReference imageStreamRef = RandomAccessStreamReference.CreateFromFile(this.imageFile); 
      requestData.Properties.Thumbnail = imageStreamRef; 
      requestData.SetBitmap(imageStreamRef); 
      succeeded = true; 
     } 
     else 
     { 
      request.FailWithDisplayText("Select an image you would like to share and try again."); 
     } 
     return succeeded; 
    } 
+1

鏈接中的另一個複製粘貼答案。這可能是我在你的答案中找到的第10個。請考慮如何更正確地回答您的提問者,或者更確切地說,我會說。複製粘貼的答案只值得評論。 – Ian

+0

源代碼更新 –

+1

如果您從別處複製/粘貼代碼,您必須**清楚它不是由您編寫的。僅添加鏈接是不夠的。使用[引用格式](http://stackoverflow.com/help/formatting)(用'>'開始行)將您的文字與您複製的文字區分開來。 –