Share via


LauncherOptions.ContentType 屬性

定義

取得或設定與表示網路上檔案的 URI 相關聯的內容類型。

public:
 property Platform::String ^ ContentType { Platform::String ^ get(); void set(Platform::String ^ value); };
winrt::hstring ContentType();

void ContentType(winrt::hstring value);
public string ContentType { get; set; }
var string = launcherOptions.contentType;
launcherOptions.contentType = string;
Public Property ContentType As String

屬性值

String

Platform::String

winrt::hstring

URI 的內容類型。

範例

呼叫 Launcher.LaunchUriAsync (Uri、LauncherOptions) 方法, 並將 ContentType 設定為與所啟動 URI 相關聯的內容類型。

// The URI to launch
string uriToLaunch = @"http://www.contoso.com/SomeFile.docx";
var uri = new Uri(uriToLaunch);

async void DefaultLaunch()
{
   // Set the URI's content type
   var options = new Windows.System.LauncherOptions();
   options.ContentType = "application/vnd.ms-word.document.12";

   // Launch the URI with the content type
   var success = await Windows.System.Launcher.LaunchUriAsync(uri, options);

   if (success)
   {
      // URI launched
   }
   else
   {
      // URI launch failed
   }
}
// The URI to launch.
Windows::Foundation::Uri m_uri{ L"http://www.contoso.com/SomeFile.docx" };

Windows::Foundation::IAsyncAction MainPage::DefaultLaunch()
{
    // Set the URI's content type.
    Windows::System::LauncherOptions launcherOptions;
    launcherOptions.ContentType(L"application/vnd.ms-word.document.12");

    // Launch the URI.
    if (co_await Windows::System::Launcher::LaunchUriAsync(m_uri, launcherOptions))
    {
        // URI launched.
    }
    else
    {
        // URI launch failed.
    }
}
// The URI to launch.
auto uri = ref new Windows::Foundation::Uri("http://www.contoso.com/SomeFile.docx");

void MainPage::DefaultLaunch()
{
   // Set the URI's content type
   auto launchOptions = ref new Windows::System::LauncherOptions();
   launchOptions->ContentType = "application/vnd.ms-word.document.12";

   // Launch the URI with the content type
   concurrency::task<bool> launchUriOperation(Windows::System::Launcher::LaunchUriAsync(uri, launchOptions));
   launchUriOperation.then([](bool success)
   {
      if (success)
      {
         // URI launched
      }
      else
      {
         // URI launch failed
      }
   });
}
' The URI to launch
Dim uri As New Uri("http://www.contoso.com/SomeFile.docx")

async Sub DefaultLaunch()

   ' Set the URI's content type
   Dim options = Windows.System.LauncherOptions()
   options.ContentType = "application/vnd.ms-word.document.12"

   ' Launch the URI with the content type
   Dim success = await Windows.System.Launcher.LaunchUriAsync(uri, options)

   If success Then
      ' URI launched
   Else
      ' URI launch failed
   End If

End Sub

備註

只有在使用 Launcher.LaunchUriAsync (Uri、LauncherOptions) 啟動 URI 時,才能指定 ContentType。

ContentType屬性可讓您的應用程式指定 URI 以及內容類型。 您可以使用這個來建立指向網路上資源的 URI 與檔案類型的關聯,而不是 URI 配置名稱。 Windows 會嘗試使用從內容類型計算的檔案類型來選取要啟動的應用程式。 預設檔案處理常式接著會傳遞 URI,而不是檔案路徑。 例如,如果您有指向 .docx 檔案的 HTTP:// URI,按一下它通常會開啟瀏覽器並開始下載檔案。 藉由使用 ContentType 屬性,您可以略過中繼步驟,並立即啟動預設的檔案處理常式。 然後,檔案處理常式就可以使用內嵌在 URI 中的路徑,直接存取網路上的檔案。

如果處理常式無法直接在 URI 上運作,則會代表他們下載檔案複本。

由於 ContentType 可讓您直接啟動檔案處理常式,套用至檔案啟動的相同安全性檢查會套用至指定此選項的 URI 啟動。 如需這些安全性檢查的詳細資訊 ,請參閱啟動檔案的預設應用程式

注意

只有在預設檔案處理常式支援將 URI 傳遞至網路上的檔案時,這項功能才能運作。 預設檔案處理常式也必須能夠向檔案的伺服器進行驗證。 由於這些限制,只有在您已徹底測試應用程式與預期要處理所啟動檔案的應用程式之間的端對端案例時,才應該使用 ContentType 屬性

重要

此屬性只會在桌面裝置上實作。

適用於

另請參閱