Catatan
Akses ke halaman ini memerlukan otorisasi. Anda dapat mencoba masuk atau mengubah direktori.
Akses ke halaman ini memerlukan otorisasi. Anda dapat mencoba mengubah direktori.
Pelajari cara meluncurkan aplikasi default untuk file dari WinUI, Universal Windows Platform (UWP), atau aplikasi desktop lainnya. Banyak aplikasi perlu bekerja dengan file yang tidak dapat mereka tangani sendiri. Misalnya, aplikasi email menerima berbagai jenis file dan memerlukan cara untuk meluncurkan file-file ini di handler default mereka. Langkah-langkah ini menunjukkan cara menggunakan API Windows.System.Launcher Windows Runtime (WinRT) untuk meluncurkan handler default untuk file yang tidak dapat ditangani aplikasi Anda sendiri.
API penting
API berikut ditampilkan dalam topik ini:
Nota
Kecuali disebutkan sebaliknya, semua API WinRT yang digunakan dalam topik ini dapat digunakan di aplikasi UWP, aplikasi WinUI, dan aplikasi desktop lainnya. Untuk membaca selengkapnya tentang mengaktifkan aplikasi desktop Anda agar berfungsi dengan API WinRT, lihat Memanggil Windows Runtime API dalam aplikasi desktop.
Mendapatkan sebuah objek file
Pertama, dapatkan objek Windows.Storage.StorageFile untuk file tersebut.
Jika file disertakan dalam paket untuk aplikasi Anda, Anda dapat menggunakan properti Package.InstalledLocation untuk mendapatkan objek Windows.Storage.StorageFolder dan metode Windows.Storage.StorageFolder.GetFileAsync untuk mendapatkan objek StorageFile.
Jika file berada di folder yang diketahui, Anda dapat menggunakan properti kelas Windows.Storage.KnownFolders untuk mendapatkan StorageFolder dan metode GetFileAsync untuk mendapatkan objek StorageFile.
Luncurkan file
Windows menyediakan beberapa opsi berbeda untuk meluncurkan handler default untuk file. Opsi ini dijelaskan dalam bagan ini dan di bagian berikut.
Pilihan | Metode | Deskripsi |
---|---|---|
Peluncuran bawaan | LaunchFileAsync(IStorageFile) | Luncurkan file yang ditentukan dengan handler default. |
Buka Dengan peluncuran | LaunchFileAsync(IStorageFile, LauncherOptions) | Luncurkan file yang ditentukan yang memungkinkan pengguna memilih handler melalui dialog Buka Dengan. |
Luncurkan dengan aplikasi alternatif yang direkomendasikan | LaunchFileAsync(IStorageFile, LauncherOptions) | Luncurkan file yang ditentukan dengan handler default. Jika tidak ada handler yang diinstal pada sistem, merekomendasikan aplikasi di toko kepada pengguna. |
Luncurkan dengan tampilan tersisa yang diinginkan | LaunchFileAsync(IStorageFile, LauncherOptions) (khusus Windows) | Luncurkan file yang ditentukan dengan handler default. Tentukan preferensi untuk tetap berada di layar setelah peluncuran dan minta ukuran jendela tertentu. LauncherOptions.DesiredRemainingView tidak didukung pada keluarga perangkat seluler. |
Peluncuran bawaan
Panggil metode Windows.System.Launcher.LaunchFileAsync(IStorageFile) untuk meluncurkan aplikasi default. Contoh ini menggunakan metode Windows.Storage.StorageFolder.GetFileAsync untuk meluncurkan file gambar, test.png, yang disertakan 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()
{
auto installFolder{ Windows::ApplicationModel::Package::Current().InstalledLocation() };
Windows::Storage::StorageFile file{ co_await installFolder.GetFileAsync(L"images\\test.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
{
// Could not 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
}
});
}
Buka Dengan peluncuran
Panggil metode Windows.System.Launcher.LaunchFileAsync(IStorageFile, LauncherOptions) dengan LauncherOptions.DisplayApplicationPicker disetel ke true untuk menjalankan aplikasi yang dipilih pengguna dari kotak dialog Buka Dengan.
Sebaiknya gunakan kotak dialog Buka Dengan saat pengguna mungkin ingin memilih aplikasi selain default untuk file tertentu. Misalnya, jika aplikasi Anda memungkinkan pengguna meluncurkan file gambar, handler default kemungkinan akan menjadi aplikasi penampil. Dalam beberapa kasus, pengguna mungkin ingin mengedit gambar alih-alih melihatnya. Gunakan opsi Buka Dengan bersama dengan perintah alternatif di AppBar atau di menu konteks untuk memungkinkan pengguna memunculkan dialog Buka Dengan dan memilih aplikasi editor dalam jenis skenario ini.
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()
{
auto installFolder{ Windows::ApplicationModel::Package::Current().InstalledLocation() };
Windows::Storage::StorageFile file{ co_await installFolder.GetFileAsync(L"images\\test.png") };
if (file)
{
// Set the option to show the picker
Windows::System::LauncherOptions launchOptions;
launchOptions.DisplayApplicationPicker(true);
// Launch the retrieved file
bool success = co_await Windows::System::Launcher::LaunchFileAsync(file, launchOptions);
if (success)
{
// File launched
}
else
{
// File launch failed
}
}
else
{
// Could not 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
}
});
}
Luncurkan dengan aplikasi yang direkomendasikan sebagai fallback
Dalam beberapa kasus, pengguna mungkin tidak memiliki aplikasi yang terinstal untuk menangani file yang Anda luncurkan. Secara default, Windows akan menangani kasus-kasus ini dengan menyediakan tautan kepada pengguna untuk mencari aplikasi yang sesuai di toko. Jika Anda ingin memberi pengguna rekomendasi khusus untuk aplikasi mana yang akan diperoleh dalam skenario ini, Anda dapat melakukannya dengan meneruskan rekomendasi tersebut bersama dengan file yang Anda luncurkan. Untuk melakukan ini, gunakan metode Windows.System.Launcher.launchFileAsync(IStorageFile, LauncherOptions) dengan LauncherOptions.PreferredApplicationPackageFamilyName diatur ke nama keluarga paket dari aplikasi di Store yang ingin Anda rekomendasikan. Kemudian, atur LauncherOptions.PreferredApplicationDisplayName ke nama aplikasi tersebut. Windows akan menggunakan informasi ini untuk menggantikan opsi umum untuk mencari aplikasi di toko dengan opsi tertentu untuk memperoleh aplikasi yang direkomendasikan dari Store.
Nota
Anda harus mengatur kedua opsi ini untuk merekomendasikan aplikasi. Mengatur satu tanpa yang lain akan mengakibatkan kegagalan.
async void DefaultLaunch()
{
// Path to the file in the app package to launch
string imageFile = @"images\test.contoso";
// Get the image file from the package's image directory
var file = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync(imageFile);
if (file != null)
{
// Set the recommended app
var options = new Windows.System.LauncherOptions();
options.PreferredApplicationPackageFamilyName = "Contoso.FileApp_8wknc82po1e";
options.PreferredApplicationDisplayName = "Contoso File App";
// Launch the retrieved file pass in the recommended app
// in case the user has no apps installed to handle the 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()
{
auto installFolder{ Windows::ApplicationModel::Package::Current().InstalledLocation() };
Windows::Storage::StorageFile file{ co_await installFolder.GetFileAsync(L"images\\test.png") };
if (file)
{
// Set the recommended app
Windows::System::LauncherOptions launchOptions;
launchOptions.PreferredApplicationPackageFamilyName(L"Contoso.FileApp_8wknc82po1e");
launchOptions.PreferredApplicationDisplayName(L"Contoso File App");
// Launch the retrieved file, and pass in the recommended app
// in case the user has no apps installed to handle the file.
bool success = co_await Windows::System::Launcher::LaunchFileAsync(file, launchOptions);
if (success)
{
// File launched
}
else
{
// File launch failed
}
}
else
{
// Could not find file
}
}
void MainPage::DefaultLaunch()
{
auto installFolder = Windows::ApplicationModel::Package::Current->InstalledLocation;
concurrency::task<Windows::Storage::StorageFile^> getFileOperation(installFolder->GetFileAsync("images\\test.contoso"));
getFileOperation.then([](Windows::Storage::StorageFile^ file)
{
if (file != nullptr)
{
// Set the recommended app
auto launchOptions = ref new Windows::System::LauncherOptions();
launchOptions->PreferredApplicationPackageFamilyName = "Contoso.FileApp_8wknc82po1e";
launchOptions->PreferredApplicationDisplayName = "Contoso File App";
// Launch the retrieved file pass, and in the recommended app
// in case the user has no apps installed to handle the 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
}
});
}
Luncurkan dengan Tampilan Tersisa yang Diinginkan (khusus UWP)
Aplikasi sumber yang memanggil LaunchFileAsync dapat meminta agar mereka tetap berada di layar setelah peluncuran file. Secara default, Windows mencoba berbagi semua ruang yang tersedia secara merata antara aplikasi sumber dan aplikasi target yang menangani file. Aplikasi sumber dapat menggunakan properti DesiredRemainingView untuk menunjukkan kepada sistem operasi bahwa mereka lebih suka jendela aplikasi mereka untuk mengambil lebih banyak atau kurang dari ruang yang tersedia. DesiredRemainingView juga dapat digunakan untuk menunjukkan bahwa aplikasi sumber tidak perlu tetap berada di layar setelah peluncuran file dan dapat sepenuhnya digantikan oleh aplikasi target. Properti ini hanya menentukan ukuran jendela pilihan dari aplikasi panggilan. Ini tidak menentukan perilaku aplikasi lain yang mungkin juga ada di layar secara bersamaan.
Nota
Windows memperhitungkan beberapa faktor berbeda ketika menentukan ukuran jendela akhir aplikasi sumber, misalnya, preferensi aplikasi sumber, jumlah aplikasi di layar, orientasi layar, dan sebagainya. Dengan mengatur DesiredRemainingView, Anda tidak dijamin mendapatkan perilaku jendela yang spesifik untuk aplikasi sumber.
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 desired remaining view
var options = new Windows.System.LauncherOptions();
options.DesiredRemainingView = Windows.UI.ViewManagement.ViewSizePreference.UseLess;
// 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()
{
auto installFolder{ Windows::ApplicationModel::Package::Current().InstalledLocation() };
Windows::Storage::StorageFile file{ co_await installFolder.GetFileAsync(L"images\\test.png") };
if (file)
{
// Set the desired remaining view.
Windows::System::LauncherOptions launchOptions;
launchOptions.DesiredRemainingView(Windows::UI::ViewManagement::ViewSizePreference::UseLess);
// Launch the retrieved file.
bool success = co_await Windows::System::Launcher::LaunchFileAsync(file, launchOptions);
if (success)
{
// File launched
}
else
{
// File launch failed
}
}
else
{
// Could not 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 desired remaining view.
auto launchOptions = ref new Windows::System::LauncherOptions();
launchOptions->DesiredRemainingView = Windows::UI::ViewManagement::ViewSizePreference::UseLess;
// 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
}
});
}
Komentar
Aplikasi Anda tidak dapat memilih aplikasi yang diluncurkan. Pengguna menentukan aplikasi mana yang diluncurkan. Pengguna dapat memilih aplikasi UWP atau aplikasi desktop Windows.
Saat meluncurkan file, aplikasi Anda harus menjadi aplikasi latar depan, yaitu harus terlihat oleh pengguna. Persyaratan ini membantu memastikan bahwa pengguna tetap memegang kendali. Untuk memenuhi persyaratan ini, pastikan Anda mengikat semua peluncuran file langsung ke UI aplikasi Anda. Kemungkinan besar, pengguna harus selalu mengambil beberapa tindakan untuk memulai peluncuran file.
Anda tidak dapat meluncurkan jenis file yang berisi kode atau skrip jika dijalankan secara otomatis oleh sistem operasi, seperti, file .exe, .msi, dan .js. Pembatasan ini melindungi pengguna dari file yang berpotensi berbahaya yang dapat mengubah sistem operasi. Anda dapat menggunakan metode ini untuk meluncurkan jenis file yang dapat berisi skrip jika dijalankan oleh aplikasi yang mengisolasi skrip, seperti, .docx file. Aplikasi seperti Microsoft Word menyimpan skrip dalam file .docx agar tidak mengubah sistem operasi.
Jika Anda mencoba meluncurkan jenis file terbatas, peluncuran akan gagal dan panggilan balik kesalahan Anda akan dipanggil. Jika aplikasi Anda menangani berbagai jenis file dan Anda mengantisipasi munculnya kesalahan ini, kami sarankan Anda memberikan pengalaman alternatif kepada pengguna Anda. Misalnya, Anda dapat memberi pengguna opsi untuk menyimpan file ke desktop, dan mereka dapat membukanya di sana.
Konten terkait
Windows developer