SharedStorageAccessManager 類別

定義

透過 Uri 啟用、應用程式服務、REST API 等方式傳遞權杖,讓應用程式與另一個應用程式共用檔案。目標應用程式會兌換權杖,以取得來源應用程式共用的檔案。

public ref class SharedStorageAccessManager abstract sealed
/// [Windows.Foundation.Metadata.ContractVersion(Windows.Foundation.UniversalApiContract, 65536)]
class SharedStorageAccessManager final
[Windows.Foundation.Metadata.ContractVersion(typeof(Windows.Foundation.UniversalApiContract), 65536)]
public static class SharedStorageAccessManager
Public Class SharedStorageAccessManager
繼承
Object Platform::Object IInspectable SharedStorageAccessManager
屬性

Windows 需求

裝置系列
Windows 10 (已於 10.0.10240.0 引進)
API contract
Windows.Foundation.UniversalApiContract (已於 v1.0 引進)

範例

在下列範例中,來源應用程式會啟動對應應用程式,並提供 .gpx 檔案,其中包含目標應用程式要顯示的駕駛方向。

首先,來源應用程式會取得 .gpx 檔案的權杖,並使用通訊協定啟用來啟動目標應用程式。

public async void ShareMostRecentDirections()
{
       // Get the most recently opened .gpx file
       // from the recently used file list.
       StorageItemMostRecentlyUsedList mostRecent = 
           StorageApplicationPermissions.MostRecentlyUsedList;

       String mruToken = mostRecent.Entries.FirstOrDefault().Token;
       StorageFile file = await mostRecent.GetFileAsync(mruToken);

       // Get the token to share access to the updated .gpx file.
       String sharingToken = SharedStorageAccessManager.AddFile(file);

       //Launch the driving application .
       Uri driveTo = new Uri("nokia-drive-to:?Gpxfile=" + sharingToken);
       var launch = await Launcher.LaunchURIAsync(driveTo);
}

接下來,目標應用程式會提供從來源應用程式收到的權杖,以取得 .gpx 檔案。

protected override async void OnActivated(IActivatedEventArgs args)
{
    var protocolArgs = args as ProtocolActivatedEventArgs;

    // Get the token from the URI.
    var queryStrings = new WwwFormUrlDecoder(protocolArgs.Uri.Query);
    string gpxFileToken = queryStrings.GetFirstValueByName("GpxFile");

    // Get the .gpx file and call a custom method
    // to display driving directions.
    if (!string.IsNullOrEmpty(gpxFileToken))
    {
        PlotGpxFile(await
            SharedStorageAccessManager.RedeemTokenForFileAsync(gpxFileToken));
    }
}

備註

以下是一連串的步驟,可讓應用程式透過傳遞權杖作為 Uri 啟用的一部分,來與另一個應用程式共用檔案。

  • 來源應用程式會呼叫 AddFile 方法,以取得其傳遞至目標應用程式的共用權杖,其會以 Uri 啟動。
  • 目標應用程式會呼叫 RedeemTokenForFileAsync 方法來取得共用檔案。
  • 或者,來源應用程式可以呼叫 RemoveFile 方法,藉由呼叫 AddFile 方法撤銷先前取得的權杖。 如需 URI 啟用的詳細資訊,請參閱 啟動 URI 的預設應用程式

使用 SharedStorageAccessManager 類別和共用權杖受限於下列需求和限制。

  • 共用權杖只能兌換一次。 之後,權杖就不再有效。
  • 共用權杖會在 14 天后到期,且不再有效。
  • 來源應用程式無法取得一千個以上的共用權杖。 不過,在兌換、移除或到期權杖之後,它不再計入來源應用程式的配額。

此類別不支援網路檔案。

方法

AddFile(IStorageFile)

取得共用權杖,可讓應用程式與其他應用程式共用指定的檔案。

RedeemTokenForFileAsync(String)

藉由提供從來源應用程式收到的共用權杖,取得另一個應用程式所共用的檔案。

RemoveFile(String)

撤銷現有的共用權杖。

適用於