使用自動播放功能來自動啟動

當使用者將裝置連接到 PC 時,您可以使用 AutoPlay 來提供您的應用程式作為選項。 這些裝置包含非磁碟區型裝置 (例如相機或媒體播放裝置) 或磁碟區型裝置 (例如 USB 隨身碟、SD 記憶卡或 DVD)。 當使用者透過鄰近度 (點擊) 在兩台電腦之間共用檔案時,您也可以使用 AutoPlay 來提供您的應用程式作為一個選項。

注意

如果您是裝置製造商,而且想要將 Microsoft Store 裝置應用程式 關聯為裝置的 AutoPlay 處理程式,您可以在裝置中繼資料中識別該應用程式。 如需詳細資訊,請參閱 Microsoft Store 裝置應用程式的 AutoPlay

註冊 AutoPlay 內容

您可以將應用程式註冊為 AutoPlay 內容事件的選項。 將磁碟區裝置 (例如,相機記憶卡、隨身碟或 DVD) 插入電腦時,就會引發 AutoPlay 事件。 在這裡,我們會示範如何在插入相機的磁碟區裝置時,將您的應用程式識別為 AutoPlay 選項。

在本教程中,您建立了一個顯示圖像檔案或將其複製到圖片的應用程式。 您註冊了 AutoPlay ShowPicturesOnArrival 內容事件的應用程式。

自動播放也會針對使用鄰近性 (點選) 的電腦之間共用的內容引發內容事件。 您可以使用本節中的步驟和程式碼來處理使用鄰近性的電腦之間共用的檔案。 下表列出可使用鄰近性共享內容的 AutoPlay 內容事件。

動作 AutoPlay 內容事件
共用音樂 PlayMusicFilesOnArrival
共用影片 PlayVideoFilesOnArrival

使用鄰近性共用檔案時,FileActivatedEventArgs 物件的 Files 屬性會包含包含所有共用檔案之根資料夾的參考。

步驟 1:建立新專案並新增自動播放宣告

  1. 開啟 Microsoft Visual Studio,然後從檔案 功能表中選取新專案。 在 Visual C# 部分的 Windows 下,選擇空白應用程式 (通用 Windows)。 將應用程式 命名為 AutoPlayDisplayOrCopyImages,然後按兩下確定。
  2. 開啟 Package.appxmanifest 檔案並選擇功能標籤。 選擇可移動儲存圖片庫功能。 這可讓應用程式存取抽取式儲存設備以取得相機記憶體,以及存取本機圖片。
  3. 在清單檔案中,選擇聲明標籤。在可用聲明下拉清單中,選擇 AutoPlay 內容並按一下新增。 選擇已新增至支援聲明清單中的新 AutoPlay 內容項目。
  4. 當 AutoPlay 引發內容事件時,自動播放內容宣告會將您的應用程式識別為選項。 此事件是根據磁碟區裝置的內容,例如 DVD 或拇指磁碟驅動器。 自動播放會檢查磁碟區裝置的內容,並判斷要引發的內容事件。 如果磁碟區的根目錄包含 DCIM、AVCHD 或 PRIVATE\ACHD 資料夾,或使用者已啟用選擇自動播放控制台中的每種類型媒體的處理方式且圖片位於磁碟區的根目錄中,則自動播放引發 ShowPicturesOnArrival 事件。 在啟動動作區段中,針對第一個啟動動作,輸入下方表格 1 的值。
  5. AutoPlay 內容項目的啟動操作部分中,按一下新增以新增第二個啟動操作。 針對第二個啟動動作,輸入下表 2 中的值。
  6. 可用聲明下拉清單中,選擇檔案類型關聯並按一下新增。 在新檔案類型關聯聲明的屬性中,將顯示名稱欄位設定為 AutoPlay 複製或顯示影像,並將名稱欄位設為 image_association1。 在支持的檔類型區段中,按兩下新增。 將檔案類型欄位設定為 .jpg。 在支援的檔類型區段中,將新檔案關聯的檔案類型欄位設定.png。 針對內容事件,AutoPlay 會篩選出未明確與應用程式相關聯的任何文件類型。
  7. 儲存並關閉資訊清單檔。

資料表 1

設定
動詞命令 show
動作顯示名稱 顯示圖片
內容事件 ShowPicturesOnArrival

動作顯示名稱設定會識別應用程式自動播放顯示的字串。 Verb 設定會識別針對選取的選項而傳遞至您的應用程式的值。 您可以為自動播放事件指定多個啟動動作,並使用 Verb 設定判斷使用者為應用程式選取的選項。 您可以檢查傳遞至應用程式啟動事件引數的 Verb 屬性,以判斷使用者選取的選項。 您可以為 Verb 設定使用保留字 open 以外的任何值。

表格 2

設定
動詞命令 copy
動作顯示名稱 將圖片複製到媒體櫃
內容事件 ShowPicturesOnArrival

步驟 2:新增 XAML UI

開啟 MainPage.xaml 檔案,並將下列 XAML 新增至預設 <Grid> 區段。

<TextBlock FontSize="18">File List</TextBlock>
<TextBlock x:Name="FilesBlock" HorizontalAlignment="Left" TextWrapping="Wrap"
           VerticalAlignment="Top" Margin="0,20,0,0" Height="280" Width="240" />
<Canvas x:Name="FilesCanvas" HorizontalAlignment="Left" VerticalAlignment="Top"
        Margin="260,20,0,0" Height="280" Width="100"/>

步驟 3:新增初始化程序代碼

此步驟中的程式碼檢查 Verb 屬性中的動詞值,該值是在 OnFileActivated 事件期間傳遞給應用程式的啟動參數之一。 然後,程式代碼會呼叫與用戶選取之選項相關的方法。 針對相機記憶體事件,AutoPlay 會將相機記憶體的根資料夾傳遞給應用程式。 您可以從 Files 屬性的第一個項目擷取此資料夾。

開啟 App.xaml.cs 檔案並將下列程式碼新增至 App 類別。

protected override void OnFileActivated(FileActivatedEventArgs args)
{
    if (args.Verb == "show")
    {
        Frame rootFrame = (Frame)Window.Current.Content;
        MainPage page = (MainPage)rootFrame.Content;

        // Call DisplayImages with root folder from camera storage.
        page.DisplayImages((Windows.Storage.StorageFolder)args.Files[0]);
    }

    if (args.Verb == "copy")
    {
        Frame rootFrame = (Frame)Window.Current.Content;
        MainPage page = (MainPage)rootFrame.Content;

        // Call CopyImages with root folder from camera storage.
        page.CopyImages((Windows.Storage.StorageFolder)args.Files[0]);
    }

    base.OnFileActivated(args);
}

注意DisplayImagesCopyImages 方法在以下步驟中新增。

步驟 4:新增程式代碼以顯示影像

開啟 MainPage.xaml.cs 檔案並將下列程式碼新增至 MainPage 類別。

async internal void DisplayImages(Windows.Storage.StorageFolder rootFolder)
{
    // Display images from first folder in root\DCIM.
    var dcimFolder = await rootFolder.GetFolderAsync("DCIM");
    var folderList = await dcimFolder.GetFoldersAsync();
    var cameraFolder = folderList[0];
    var fileList = await cameraFolder.GetFilesAsync();
    for (int i = 0; i < fileList.Count; i++)
    {
        var file = (Windows.Storage.StorageFile)fileList[i];
        WriteMessageText(file.Name + "\n");
        DisplayImage(file, i);
    }
}

async private void DisplayImage(Windows.Storage.IStorageItem file, int index)
{
    try
    {
        var sFile = (Windows.Storage.StorageFile)file;
        Windows.Storage.Streams.IRandomAccessStream imageStream =
            await sFile.OpenAsync(Windows.Storage.FileAccessMode.Read);
        Windows.UI.Xaml.Media.Imaging.BitmapImage imageBitmap =
            new Windows.UI.Xaml.Media.Imaging.BitmapImage();
        imageBitmap.SetSource(imageStream);
        var element = new Image();
        element.Source = imageBitmap;
        element.Height = 100;
        Thickness margin = new Thickness();
        margin.Top = index * 100;
        element.Margin = margin;
        FilesCanvas.Children.Add(element);
    }
    catch (Exception e)
    {
       WriteMessageText(e.Message + "\n");
    }
}

// Write a message to MessageBlock on the UI thread.
private Windows.UI.Core.CoreDispatcher messageDispatcher = Window.Current.CoreWindow.Dispatcher;

private async void WriteMessageText(string message, bool overwrite = false)
{
    await messageDispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal,
        () =>
        {
            if (overwrite)
                FilesBlock.Text = message;
            else
                FilesBlock.Text += message;
        });
}

步驟 5:新增程式代碼以複製映像

開啟 MainPage.xaml.cs 檔案並將下列程式碼新增至 MainPage 類別。

async internal void CopyImages(Windows.Storage.StorageFolder rootFolder)
{
    // Copy images from first folder in root\DCIM.
    var dcimFolder = await rootFolder.GetFolderAsync("DCIM");
    var folderList = await dcimFolder.GetFoldersAsync();
    var cameraFolder = folderList[0];
    var fileList = await cameraFolder.GetFilesAsync();

    try
    {
        var folderName = "Images " + DateTime.Now.ToString("yyyy-MM-dd HHmmss");
        Windows.Storage.StorageFolder imageFolder = await
            Windows.Storage.KnownFolders.PicturesLibrary.CreateFolderAsync(folderName);

        foreach (Windows.Storage.IStorageItem file in fileList)
        {
            CopyImage(file, imageFolder);
        }
    }
    catch (Exception e)
    {
        WriteMessageText("Failed to copy images.\n" + e.Message + "\n");
    }
}

async internal void CopyImage(Windows.Storage.IStorageItem file,
                              Windows.Storage.StorageFolder imageFolder)
{
    try
    {
        Windows.Storage.StorageFile sFile = (Windows.Storage.StorageFile)file;
        await sFile.CopyAsync(imageFolder, sFile.Name);
        WriteMessageText(sFile.Name + " copied.\n");
    }
    catch (Exception e)
    {
        WriteMessageText("Failed to copy file.\n" + e.Message + "\n");
    }
}

步驟 6:使用建置並執行應用程式

  1. 按 F5 建置並部署應用程式 (在調試模式下)。
  2. 若要執行您的應用程式,請將相機記憶體卡或其他儲存設備從相機插入您的電腦。 然後,從自動播放選項清單中選擇您在 package.appxmanifest 檔案中指定的內容事件選項之一。 此範例程式碼僅顯示或複製相機記憶卡的 DCIM 資料夾中的圖片。 如果您的相機記憶卡將圖片儲存在 AVCHD 或 PRIVATE\ACHD 資料夾中,您將需要相應地更新程式碼。

注意

如果您沒有相機記憶卡,且根目錄中有一個名為 DCIM 的資料夾,並且該 DCIM 資料夾有一個包含影像的子資料夾,則可以使用隨身碟。

註冊 AutoPlay 裝置

您可以將應用程式註冊為 AutoPlay 裝置事件的選項。 將裝置連接到電腦時,就會引發 AutoPlay 裝置事件。

在這裡,我們會示範如何在相機連線到計算機時,將您的應用程式識別為 AutoPlay 選項。 應用程式會註冊為 WPD\ImageSourceAutoPlay 事件的處理程式。 這是 Windows 可攜式裝置 (WPD) 系統在相機和其他映像裝置使用 MTP 通知其為 ImageSource 時引發的常見事件。 如需詳細資訊,請參閱 Windows 可攜式裝置

重要 Windows.Devices.Portable.儲存體裝置 API 是傳統型裝置系列的一部分。 應用程式只能在桌面裝置系列中的 Windows 10 裝置上使用這些 API,例如電腦。

步驟 1:建立另一個新專案並新增自動播放宣告

  1. 開啟 Visual Studio 並從檔案功能表中選擇新專案。 在 Visual C# 部分的 Windows 下,選擇空白應用程式 (通用 Windows)。 將應用程式命名為 AutoPlayDevice_Camera,然後按兩下確定。
  2. 開啟 Package.appxmanifest 檔案並選擇功能標籤。 選擇可移動儲存功能。 這可讓應用程式以卸除式儲存磁碟區裝置的形式存取相機上的資料。
  3. 在清單檔案中,選擇聲明標籤。在可用聲明下拉清單中,選擇 AutoPlay 設備並按一下新增。 選擇已新增至支援聲明清單中的新 AutoPlay 裝置項目。
  4. AutoPlay 引發已知事件的裝置事件時,自動播放裝置宣告會將您的應用程式識別為選項。 在啟動動作區段中,針對第一個啟動動作,輸入下方表格 1 的值。
  5. 可用聲明下拉清單中,選擇檔案類型關聯並按一下新增。 在新檔案類型關聯聲明的屬性中,將顯示名稱欄位設定為顯示來自相機的影像,將名稱欄位設為 camera_association1。 在支持的檔類型區段中,按兩下新增 (如需)。 將檔案類型欄位設定為 .jpg。 在支持的檔類型區段中,再按兩下新增。 將新檔案關聯的檔案類型欄位設定為 .png。 針對內容事件,AutoPlay 會篩選出未明確與應用程式相關聯的任何文件類型。
  6. 儲存並關閉資訊清單檔。
設定
動詞命令 show
動作顯示名稱 顯示圖片
內容事件 WPD\ImageSource

動作顯示名稱設定會識別應用程式自動播放顯示的字串。 Verb 設定會識別針對選取的選項而傳遞至您的應用程式的值。 您可以為自動播放事件指定多個啟動動作,並使用 Verb 設定判斷使用者為應用程式選取的選項。 您可以檢查傳遞至應用程式啟動事件引數的 Verb 屬性,以判斷使用者選取的選項。 您可以為 Verb 設定使用保留字 open 以外的任何值。 如需在單一應用程式中使用多個動詞的範例,請參閱註冊自動播放內容

步驟 2:新增桌面延伸模組的元件參考

存取 Windows 可攜式裝置上的儲存所需的 API Windows.Devices.Portable.StorageDevice 是桌面裝置系列的一部分。 這表示需要使用 API 的特殊元件,這些呼叫只能在桌面裝置系列 (例如電腦) 的裝置上運作。

  1. 方案總管 中,以滑鼠右鍵按一下參考,然後選擇新增參考
  2. 展開通用 Windows,然後按兩下擴充功能
  3. 然後選取 UWP 的 Windows 桌面延伸模組,然後按兩下確定

步驟 3:新增 XAML UI

開啟 MainPage.xaml 檔案,並將下列 XAML 新增至預設 <Grid> 區段。

<StackPanel Orientation="Vertical" Margin="10,0,-10,0">
    <TextBlock FontSize="24">Device Information</TextBlock>
    <StackPanel Orientation="Horizontal">
        <TextBlock x:Name="DeviceInfoTextBlock" FontSize="18" Height="400" Width="400" VerticalAlignment="Top" />
        <ListView x:Name="ImagesList" HorizontalAlignment="Left" Height="400" VerticalAlignment="Top" Width="400">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Vertical">
                        <Image Source="{Binding Path=Source}" />
                        <TextBlock Text="{Binding Path=Name}" />
                    </StackPanel>
                </DataTemplate>
            </ListView.ItemTemplate>
            <ListView.ItemsPanel>
                <ItemsPanelTemplate>
                    <WrapGrid Orientation="Horizontal" ItemHeight="100" ItemWidth="120"></WrapGrid>
                </ItemsPanelTemplate>
            </ListView.ItemsPanel>
        </ListView>
    </StackPanel>
</StackPanel>

步驟 4:新增啟用程序代碼

此步驟中的程式碼透過將相機的裝置資訊 Id 傳遞給 FromId 方法來將相機引用為 StorageDevice。 相機的裝置資訊標識碼是先將事件自變數轉換成 DeviceActivatedEventArgs 來取得,然後從 DeviceInformationId 屬性取得值。

開啟 App.xaml.cs 檔案並將下列程式碼新增至 App 類別。

protected override void OnActivated(IActivatedEventArgs args)
{
   if (args.Kind == ActivationKind.Device)
   {
      Frame rootFrame = null;
      // Ensure that the current page exists and is activated
      if (Window.Current.Content == null)
      {
         rootFrame = new Frame();
         rootFrame.Navigate(typeof(MainPage));
         Window.Current.Content = rootFrame;
      }
      else
      {
         rootFrame = Window.Current.Content as Frame;
      }
      Window.Current.Activate();

      // Make sure the necessary APIs are present on the device
      bool storageDeviceAPIPresent =
      Windows.Foundation.Metadata.ApiInformation.IsTypePresent("Windows.Devices.Portable.StorageDevice");

      if (storageDeviceAPIPresent)
      {
         // Reference the current page as type MainPage
         var mPage = rootFrame.Content as MainPage;

         // Cast the activated event args as DeviceActivatedEventArgs and show images
         var deviceArgs = args as DeviceActivatedEventArgs;
         if (deviceArgs != null)
         {
            mPage.ShowImages(Windows.Devices.Portable.StorageDevice.FromId(deviceArgs.DeviceInformationId));
         }
      }
      else
      {
         // Handle case where APIs are not present (when the device is not part of the desktop device family)
      }

   }

   base.OnActivated(args);
}

注意

此方法 ShowImages 會在下列步驟中新增。

步驟 5:新增程式代碼以顯示裝置資訊

您可以從 StorageDevice 類別的屬性取得相機的相關資訊。 此步驟中的程式代碼會在應用程式執行時向用戶顯示裝置名稱和其他資訊。 然後,程式代碼會呼叫 GetImageList 和 GetThumbnail 方法,您會在下一個步驟中新增此方法,以顯示儲存在相機上的影像縮圖

開啟 MainPage.xaml.cs 檔案並將下列程式碼新增至 MainPage 類別。

private Windows.Storage.StorageFolder rootFolder;

internal async void ShowImages(Windows.Storage.StorageFolder folder)
{
    DeviceInfoTextBlock.Text = "Display Name = " + folder.DisplayName + "\n";
    DeviceInfoTextBlock.Text += "Display Type =  " + folder.DisplayType + "\n";
    DeviceInfoTextBlock.Text += "FolderRelativeId = " + folder.FolderRelativeId + "\n";

    // Reference first folder of the device as the root
    rootFolder = (await folder.GetFoldersAsync())[0];
    var imageList = await GetImageList(rootFolder);

    foreach (Windows.Storage.StorageFile img in imageList)
    {
        ImagesList.Items.Add(await GetThumbnail(img));
    }
}

注意

GetImageListGetThumbnail 方法會在下列步驟中新增。

步驟 6:新增程式代碼以顯示影像

此步驟中的程式代碼會顯示相機上所儲存影像的縮圖。 程序代碼會對相機進行非同步呼叫,以取得縮圖影像。 不過,在上一個非同步呼叫完成之前,不會發生下一個非同步呼叫。 這可確保一次只向相機提出一個要求。

開啟 MainPage.xaml.cs 檔案並將下列程式碼新增至 MainPage 類別。

async private System.Threading.Tasks.Task<List<Windows.Storage.StorageFile>> GetImageList(Windows.Storage.StorageFolder folder)
{
    var result = await folder.GetFilesAsync();
    var subFolders = await folder.GetFoldersAsync();
    foreach (Windows.Storage.StorageFolder f in subFolders)
        result = result.Union(await GetImageList(f)).ToList();

    return (from f in result orderby f.Name select f).ToList();
}

async private System.Threading.Tasks.Task<Image> GetThumbnail(Windows.Storage.StorageFile img)
{
    // Get the thumbnail to display
    var thumbnail = await img.GetThumbnailAsync(Windows.Storage.FileProperties.ThumbnailMode.SingleItem,
                                                100,
                                                Windows.Storage.FileProperties.ThumbnailOptions.UseCurrentScale);

    // Create a XAML Image object bind to on the display page
    var result = new Image();
    result.Height = thumbnail.OriginalHeight;
    result.Width = thumbnail.OriginalWidth;
    result.Name = img.Name;
    var imageBitmap = new Windows.UI.Xaml.Media.Imaging.BitmapImage();
    imageBitmap.SetSource(thumbnail);
    result.Source = imageBitmap;

    return result;
}

步驟 7:使用建置並執行應用程式

  1. 按 F5 建置並部署應用程式 (在調試模式下)。
  2. 若要執行您的應用程式,請將相機連線到您的電腦。 然後從選項的 AutoPlay 列表中選取應用程式。

注意

並非所有相機都會公告 WPD\ImageSource AutoPlay 裝置事件。

設定抽取式儲存設備

當磁碟區裝置連線到計算機時,您可以識別磁碟區裝置,例如記憶體卡或拇指磁碟驅動器作為 AutoPlay 裝置。 當您想要將 AutoPlay 的特定應用程式關聯至磁碟區裝置的使用者時,這特別有用。

我們在這裡示範如何將磁碟區裝置識別為 AutoPlay 裝置。

若要將磁碟區裝置識別為 AutoPlay 裝置,請將 autorun.inf 檔案新增至裝置的根磁碟驅動器。 在 autorun.inf 檔案中,將 CustomEvent 密鑰新增至 AutoRun 區段。 當磁碟區裝置連線到電腦時,AutoPlay 會找到 autorun.inf 檔案,並將磁碟區視為裝置。 AutoPlay 會使用您為 CustomEvent 金鑰提供的名稱來建立 AutoPlay 事件。 然後,您可以建立應用程式,並將應用程式註冊為該 AutoPlay 事件的處理程式。 當裝置連線到電腦時,AutoPlay 會將您的應用程式顯示為磁碟區裝置的處理程式。 如需 autorun.inf 檔案的詳細資訊,請參閱 autorun.inf 專案

步驟 1:建立 autorun.inf 檔案

在磁碟區裝置的根磁碟驅動器中,新增名為 autorun.inf 的檔案。 開啟 autorun.inf 檔案,並新增下列文字。

[AutoRun]
CustomEvent=AutoPlayCustomEventQuickstart

步驟 2:建立新專案並新增自動播放宣告

  1. 開啟 Visual Studio 並從檔案功能表中選擇新專案。 在 Visual C# 部分的 Windows 下,選擇空白應用程式 (通用 Windows)。 將應用程式命名為 AutoPlayCustomEvent,然後按兩下確定。
  2. 開啟 Package.appxmanifest 檔案並選擇功能標籤。 選擇可移動儲存功能。 這可讓應用程式存取卸除式存儲設備上的檔案和資料夾。
  3. 在清單檔案中,選擇聲明標籤。在可用聲明下拉清單中,選擇 AutoPlay 內容並按一下新增。 選擇已新增至支援聲明清單中的新 AutoPlay 內容項目。

注意

或者,您也可以選擇為自訂 AutoPlay 事件新增 AutoPlay 裝置宣告。

  1. AutoPlay 內容事件聲明的啟動動作部分中,輸入下表中第一個啟動操作的值。
  2. 可用聲明下拉清單中,選擇檔案類型關聯並按一下新增。 在新檔案類型關聯聲明的屬性中,將顯示名稱欄位設定為顯示 .ms 檔案,將名稱欄位設為 ms_association。 在支持的檔類型區段中,按兩下新增。 將檔案類型欄位設定為 .ms。 針對內容事件,AutoPlay 會篩選出未明確與應用程式相關聯的任何文件類型。
  3. 儲存並關閉資訊清單檔。
設定
動詞命令 show
動作顯示名稱 顯示檔案
內容事件 AutoPlayCustomEventQuickstart

Content Event 值為您為 autorun.inf 檔案中的 CustomEvent 金鑰提供的文字。 動作顯示名稱設定會識別應用程式自動播放顯示的字串。 Verb 設定會識別針對選取的選項而傳遞至您的應用程式的值。 您可以為自動播放事件指定多個啟動動作,並使用 Verb 設定判斷使用者為應用程式選取的選項。 您可以檢查傳遞至應用程式啟動事件引數的 Verb 屬性,以判斷使用者選取的選項。 您可以為 Verb 設定使用保留字 open 以外的任何值。

步驟 3:新增 XAML UI

開啟 MainPage.xaml 檔案,並將下列 XAML 新增至預設 <Grid> 區段。

<StackPanel Orientation="Vertical">
    <TextBlock FontSize="28" Margin="10,0,800,0">Files</TextBlock>
    <TextBlock x:Name="FilesBlock" FontSize="22" Height="600" Margin="10,0,800,0" />
</StackPanel>

步驟 4:新增啟用碼

此步驟中的程式碼呼叫一個方法來顯示磁碟區裝置根磁碟機中的資料夾。 針對 AutoPlay 內容事件,AutoPlay 會在 OnFileActivated 事件期間傳遞至應用程式之啟動自變數中儲存裝置的根資料夾。 您可以從 Files 屬性的第一個項目擷取此資料夾。

開啟 App.xaml.cs 檔案並將下列程式碼新增至 App 類別。

protected override void OnFileActivated(FileActivatedEventArgs args)
{
    var rootFrame = Window.Current.Content as Frame;
    var page = rootFrame.Content as MainPage;

    // Call ShowFolders with root folder from device storage.
    page.DisplayFiles(args.Files[0] as Windows.Storage.StorageFolder);

    base.OnFileActivated(args);
}

注意

此方法 DisplayFiles 會在下列步驟中新增。

步驟 5:新增程式代碼以顯示資料夾

開啟 MainPage.xaml.cs 檔案並將下列程式碼新增至 MainPage 類別。

internal async void DisplayFiles(Windows.Storage.StorageFolder folder)
{
    foreach (Windows.Storage.StorageFile f in await ReadFiles(folder, ".ms"))
    {
        FilesBlock.Text += "  " + f.Name + "\n";
    }
}

internal async System.Threading.Tasks.Task<IReadOnlyList<Windows.Storage.StorageFile>>
    ReadFiles(Windows.Storage.StorageFolder folder, string fileExtension)
{
    var options = new Windows.Storage.Search.QueryOptions();
    options.FileTypeFilter.Add(fileExtension);
    var query = folder.CreateFileQueryWithOptions(options);
    var files = await query.GetFilesAsync();

    return files;
}

第 6 步:建置並執行應用程式

  1. 按 F5 建置並部署應用程式 (在調試模式下)。
  2. 若要執行您的應用程式,請將記憶卡或其他儲存設備插入您的電腦。 然後從 AutoPlay 處理程式選項清單中選取您的應用程式。

AutoPlay 事件參考

AutoPlay 系統可讓應用程式註冊各種裝置和磁碟區 (磁碟) 抵達事件。 若要註冊 AutoPlay 內容事件,您必須在套件清單中啟用可移動儲存功能。 下表顯示您可以註冊的事件,以及引發事件時。

案例 活動 描述
在相機上使用相片 WPD\ImageSource 針對識別為 Windows 可攜式裝置的相機引發,並提供 ImageSource 功能。
在音訊播放機上使用音樂 WPD\AudioSource 針對識別為 Windows 可攜式裝置且提供 AudioSource 功能的媒體播放機引發。
在視訊相機上使用影片 WPD\VideoSource 針對被識別為 Windows 可攜式裝置並提供 VideoSource 功能的攝影機而引發。
存取連接的隨身碟或外接硬碟 StorageOnArrival 當磁碟驅動器或磁碟區連線到計算機時引發。 如果磁碟機或磁碟區在磁碟根目錄中包含 DCIM、AVCHD 或 PRIVATE\ACHD 資料夾,則會引發 ShowPicturesOnArrival 事件。
使用大量儲存區的照片 (舊版) ShowPicturesOnArrival 當磁碟機或磁碟區的磁碟根目錄中包含 DCIM、AVCHD 或 PRIVATE\ACHD 資料夾時引發。 如果使用者在 AutoPlay Control Panel 中啟用了選擇如何處理每種類型的媒體,AutoPlay 將檢查連接到 PC 的磁碟區以確定磁碟上內容的類型。 找到圖片時,會引發 ShowPicturesOnArrival
使用鄰近共用接收相片 (點選並傳送) ShowPicturesOnArrival 當使用者使用鄰近性傳送內容時 (點選並傳送),自動播放會檢查共用檔案,以判斷內容類型。 找到圖片時,會引發 ShowPicturesOnArrival
使用大量儲存空間的音樂 (舊版) PlayMusicFilesOnArrival 如果使用者在 AutoPlay Control Panel 中啟用了選擇如何處理每種類型的媒體,AutoPlay 將檢查連接到 PC 的磁碟區以確定磁碟上內容的類型。 找到音樂檔案時,會引發 PlayMusicFilesOnArrival
使用鄰近共用接收音樂 (點選並傳送) PlayMusicFilesOnArrival 當使用者使用鄰近性傳送內容時 (點選並傳送),自動播放會檢查共用檔案,以判斷內容類型。 找到音樂檔案時,會引發 PlayMusicFilesOnArrival
使用大量儲存區的影片 (舊版) PlayVideoFilesOnArrival 如果使用者在 AutoPlay Control Panel 中啟用了選擇如何處理每種類型的媒體,AutoPlay 將檢查連接到 PC 的磁碟區以確定磁碟上內容的類型。 找到視訊檔案時,會引發 PlayVideoFilesOnArrival
使用鄰近共用接收影片 (點選並傳送) PlayVideoFilesOnArrival 當使用者使用鄰近性傳送內容時 (點選並傳送),自動播放會檢查共用檔案,以判斷內容類型。 找到視訊檔案時,會引發 PlayVideoFilesOnArrival
處理來自已連線裝置的混合檔案集 MixedContentOnArrival 如果使用者在 AutoPlay Control Panel 中啟用了選擇如何處理每種類型的媒體,AutoPlay 將檢查連接到 PC 的磁碟區以確定磁碟上內容的類型。 如果找不到特定內容類型 (例如圖片),則會引發 MixedContentOnArrival
使用鄰近共用處理混合的檔案集 (點選並傳送) MixedContentOnArrival 當使用者使用鄰近性傳送內容時 (點選並傳送),自動播放會檢查共用檔案,以判斷內容類型。 如果找不到特定內容類型 (例如圖片),則會引發 MixedContentOnArrival
處理光學媒體的視訊 PlayDVDMovieOnArrival
PlayBluRayOnArrival
PlayVideoCDMovieOnArrival
PlaySuperVideoCDMovieOnArrival
當磁碟插入光碟時,AutoPlay 會檢查檔案以判斷內容類型。 找到視訊檔案時,會引發對應至光磁碟類型的事件。
處理光學媒體的音樂 PlayCDAudioOnArrival
PlayDVDAudioOnArrival
當磁碟插入光碟時,AutoPlay 會檢查檔案以判斷內容類型。 找到音樂檔案時,會引發對應至光磁碟類型的事件。
播放增強型磁碟 PlayEnhancedCDOnArrival
PlayEnhancedDVDOnArrival
當磁碟插入光碟時,AutoPlay 會檢查檔案以判斷內容類型。 找到增強的磁碟時,會引發對應至光碟類型的事件。
處理可寫入的光磁碟 HandleCDBurningOnArrival
HandleDVDBurningOnArrival
HandleBDBurningOnArrival
當磁碟插入光碟時,AutoPlay 會檢查檔案以判斷內容類型。 當發現可寫盤時,引發與光碟類型相對應的事件。
處理任何其他裝置或磁碟區連線 UnknownContentOnArrival 如果發現內容不符合任何自動播放內容事件,則會針對所有事件引發。 不建議使用這個事件。 您應該只針對可以處理的特定自動播放事件註冊您的應用程式。

您可以使用磁碟區 autorun.inf 檔案中的 CustomEvent 專案,指定 AutoPlay 引發自定義自動播放內容事件。 如需詳細資訊,請參閱 Autorun.inf 專案

您可以將擴充功能新增至 app 的 package.appxmanifest 檔案,以將您的應用程式註冊為自動播放內容或自動播放裝置事件處理程式。 如果您使用的是 Visual Studio,則可以在聲明標籤中新增自 AutoPlay 內容AutoPlay 裝置聲明。如果您直接編輯應用程式的 package.appxmanifest 文件,請將 Extension 元素新增至套件清單中,指定 windows.autoPlayContentwindows.autoPlayDevice 作為類別。 例如,套件指令清單中的下列專案會新增 AutoPlay 內容延伸模組,以將應用程式註冊為 ShowPicturesOnArrival 事件的處理程式。

  <Applications>
    <Application Id="AutoPlayHandlerSample.App">
      <Extensions>
        <Extension Category="windows.autoPlayContent">
          <AutoPlayContent>
            <LaunchAction Verb="show" ActionDisplayName="Show Pictures"
                          ContentEvent="ShowPicturesOnArrival" />
          </AutoPlayContent>
        </Extension>
      </Extensions>
    </Application>
  </Applications>