Launcher.LaunchFileAsync Metode

Definisi

Overload

LaunchFileAsync(IStorageFile)

Memulai aplikasi default yang terkait dengan file yang ditentukan.

LaunchFileAsync(IStorageFile, LauncherOptions)

Memulai aplikasi default yang terkait dengan file yang ditentukan, menggunakan opsi yang ditentukan.

LaunchFileAsync(IStorageFile)

Memulai aplikasi default yang terkait dengan file yang ditentukan.

public:
 static IAsyncOperation<bool> ^ LaunchFileAsync(IStorageFile ^ file);
/// [Windows.Foundation.Metadata.Overload("LaunchFileAsync")]
 static IAsyncOperation<bool> LaunchFileAsync(IStorageFile const& file);
[Windows.Foundation.Metadata.Overload("LaunchFileAsync")]
public static IAsyncOperation<bool> LaunchFileAsync(IStorageFile file);
function launchFileAsync(file)
Public Shared Function LaunchFileAsync (file As IStorageFile) As IAsyncOperation(Of Boolean)

Parameter

file
IStorageFile

File.

Mengembalikan

Operasi peluncuran.

Atribut

Contoh

Contoh ini menggunakan LaunchFileAsync(IStorageFile) untuk meluncurkan file yang terkandung dalam paket aplikasi.

async void DefaultLaunch()
{
   // Path to the file in the app package to launch
   string imageFile = @"images\test.png";

   var file = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync(imageFile);

   if (file != null)
   {
      // Launch the retrieved file
      var success = await Windows.System.Launcher.LaunchFileAsync(file);

      if (success)
      {
         // File launched
      }
      else
      {
         // File launch failed
      }
   }
   else
   {
      // Could not find file
   }
}
Windows::Foundation::IAsyncAction MainPage::DefaultLaunch()
{
    // Get the app's installation folder.
    Windows::Storage::StorageFolder installFolder{ Windows::ApplicationModel::Package::Current().InstalledLocation() };

    Windows::Storage::StorageFile file{ co_await installFolder.GetFileAsync(L"Assets\\LockScreenLogo.scale-200.png") };

    if (file)
    {
        // Launch the retrieved file.
        bool success{ co_await Windows::System::Launcher::LaunchFileAsync(file) };
        if (success)
        {
            // File launched.
        }
        else
        {
            // File launch failed.
        }
    }
    else
    {
        // Couldn't find file.
    }
}
void MainPage::DefaultLaunch()
{
   auto installFolder = Windows::ApplicationModel::Package::Current->InstalledLocation;

   concurrency::task<Windows::Storage::StorageFile^> getFileOperation(installFolder->GetFileAsync("images\\test.png"));
   getFileOperation.then([](Windows::Storage::StorageFile^ file)
   {
      if (file != nullptr)
      {
         // Launch the retrieved file
         concurrency::task<bool> launchFileOperation(Windows::System::Launcher::LaunchFileAsync(file));
         launchFileOperation.then([](bool success)
         {
            if (success)
            {
               // File launched
            }
            else
            {
               // File launch failed
            }
         });
      }
      else
      {
         // Could not find file
      }
   });
}
async Sub DefaultLaunch()

   ' Path to the file in the app package to launch
   Dim imageFile = "images\test.png"

   Dim file = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync(imageFile)

   If file IsNot Nothing Then
      ' Launch the retrieved file
      Dim success = await Windows.System.Launcher.LaunchFileAsync(file)

      If success Then
         ' File launched
      Else
         ' File launch failed
      End If
   Else
      ' Could not find file
   End If
End Sub

Keterangan

Aplikasi panggilan harus terlihat oleh pengguna saat API dipanggil.

API ini harus dipanggil dari utas ASTA (juga dikenal sebagai utas UI).

API ini juga memberlakukan beberapa batasan pada jenis file apa yang dapat diluncurkannya. Banyak jenis file yang berisi kode yang dapat dieksekusi, misalnya file .exe, .msi, dan .js, diblokir agar tidak diluncurkan. Pembatasan ini melindungi pengguna dari file yang berpotensi berbahaya yang dapat mengubah sistem.

Ketika peluncuran gagal karena salah satu alasan di atas, API berhasil dan mengembalikan FALSE dari operasi asinkronnya. Karena tidak memiliki kemampuan untuk mengkueri apakah pembatasan di atas berlaku untuk peluncuran saat ini, aplikasi panggilan tidak boleh berasumsi bahwa peluncuran berhasil, dan harus menyediakan mekanisme fallback jika gagal. Solusi yang mungkin adalah meminta pengguna untuk menyimpan file dan mengarahkan pengguna untuk membukanya di desktop.

Untuk memungkinkan pengguna memilih aplikasi alih-alih meluncurkan aplikasi default, atur properti LauncherOptions.DisplayApplicationPicker .

Untuk menampilkan peringatan bahwa file berpotensi tidak aman, atur properti LauncherOptions.TreatAsUntrusted .

File diteruskan ke aplikasi terkait. Jika aplikasi terkait adalah aplikasi desktop, file diteruskan menggunakan mekanisme eksekusi shell.

Lihat juga

Berlaku untuk

LaunchFileAsync(IStorageFile, LauncherOptions)

Memulai aplikasi default yang terkait dengan file yang ditentukan, menggunakan opsi yang ditentukan.

public:
 static IAsyncOperation<bool> ^ LaunchFileAsync(IStorageFile ^ file, LauncherOptions ^ options);
/// [Windows.Foundation.Metadata.Overload("LaunchFileWithOptionsAsync")]
 static IAsyncOperation<bool> LaunchFileAsync(IStorageFile const& file, LauncherOptions const& options);
[Windows.Foundation.Metadata.Overload("LaunchFileWithOptionsAsync")]
public static IAsyncOperation<bool> LaunchFileAsync(IStorageFile file, LauncherOptions options);
function launchFileAsync(file, options)
Public Shared Function LaunchFileAsync (file As IStorageFile, options As LauncherOptions) As IAsyncOperation(Of Boolean)

Parameter

file
IStorageFile

File.

options
LauncherOptions

Opsi peluncuran untuk aplikasi.

Mengembalikan

Operasi peluncuran.

Atribut

Contoh

Panggil metode [Launcher.LaunchFileAsync(IStorageFile, LauncherOptions) dengan LauncherOptions.DisplayApplicationPicker diatur ke true untuk meluncurkan aplikasi yang dipilih pengguna untuk file dari kotak dialog Buka Dengan .

async void DefaultLaunch()
{
   // Path to the file in the app package to launch
   string imageFile = @"images\test.png";

   var file = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync(imageFile);

   if (file != null)
   {
      // Set the option to show the picker
      var options = new Windows.System.LauncherOptions();
      options.DisplayApplicationPicker = true;

      // Launch the retrieved file
      bool success = await Windows.System.Launcher.LaunchFileAsync(file, options);
      if (success)
      {
         // File launched
      }
      else
      {
         // File launch failed
      }
   }
   else
   {
      // Could not find file
   }
}
Windows::Foundation::IAsyncAction MainPage::DefaultLaunch()
{
    // Get the app's installation folder.
    Windows::Storage::StorageFolder installFolder{ Windows::ApplicationModel::Package::Current().InstalledLocation() };

    Windows::Storage::StorageFile file{ co_await installFolder.GetFileAsync(L"Assets\\LockScreenLogo.scale-200.png") };

    if (file)
    {
        // Set the option to show the picker.
        Windows::System::LauncherOptions launcherOptions;
        launcherOptions.DisplayApplicationPicker(true);

        // Launch the retrieved file.
        bool success{ co_await Windows::System::Launcher::LaunchFileAsync(file, launcherOptions) };
        if (success)
        {
            // File launched.
        }
        else
        {
            // File launch failed.
        }
    }
    else
    {
        // Couldn't find file.
    }
}
void MainPage::DefaultLaunch()
{
   auto installFolder = Windows::ApplicationModel::Package::Current->InstalledLocation;

   concurrency::task<Windows::Storage::StorageFile^> getFileOperation(installFolder->GetFileAsync("images\\test.png"));
   getFileOperation.then([](Windows::Storage::StorageFile^ file)
   {
      if (file != nullptr)
      {
         // Set the option to show the picker
         auto launchOptions = ref new Windows::System::LauncherOptions();
         launchOptions->DisplayApplicationPicker = true;

         // Launch the retrieved file
         concurrency::task<bool> launchFileOperation(Windows::System::Launcher::LaunchFileAsync(file, launchOptions));
         launchFileOperation.then([](bool success)
         {
            if (success)
            {
               // File launched
            }
            else
            {
               // File launch failed
            }
         });
      }
      else
      {
         // Could not find file
      }
   });
}
async Sub DefaultLaunch()

   ' Path to the file in the app package to launch
   Dim imageFile = "images\test.png"

   Dim file = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync(imageFile)

   If file IsNot Nothing Then
      ' Set the option to show the picker
      Dim options = Windows.System.LauncherOptions()
      options.DisplayApplicationPicker = True

      ' Launch the retrieved file
      Dim success = await Windows.System.Launcher.LaunchFileAsync(file, options)

      If success Then
         ' File launched
      Else
         ' File launch failed
      End If
   Else
      ' Could not find file
   End If
End Sub

Keterangan

Aplikasi panggilan harus terlihat oleh pengguna saat API dipanggil.

API ini harus dipanggil dari dalam utas ASTA (juga dikenal sebagai utas UI).

API ini juga memberlakukan beberapa batasan pada jenis file apa yang dapat diluncurkannya. Banyak jenis file yang berisi kode yang dapat dieksekusi, misalnya file .exe, .msi, dan .js, diblokir agar tidak diluncurkan. Pembatasan ini melindungi pengguna dari file yang berpotensi berbahaya yang dapat mengubah sistem.

Ketika peluncuran gagal karena salah satu alasan di atas, API berhasil dan mengembalikan FALSE dari operasi asinkronnya. Karena tidak memiliki kemampuan untuk mengkueri apakah pembatasan di atas berlaku untuk peluncuran saat ini, aplikasi panggilan tidak boleh berasumsi bahwa peluncuran berhasil, dan harus menyediakan mekanisme fallback jika gagal. Solusi yang mungkin adalah meminta pengguna untuk menyimpan file dan mengarahkan pengguna untuk membukanya di desktop.

Untuk memungkinkan pengguna memilih aplikasi alih-alih meluncurkan aplikasi default, atur properti LauncherOptions.DisplayApplicationPicker .

Untuk menampilkan peringatan bahwa file berpotensi tidak aman, atur properti LauncherOptions.TreatAsUntrusted .

File diteruskan ke aplikasi terkait. Jika aplikasi terkait adalah aplikasi desktop, file diteruskan menggunakan mekanisme eksekusi shell.

Lihat juga

Berlaku untuk