Meluncurkan aplikasi default untuk file
API penting
Pelajari cara meluncurkan aplikasi default untuk file. 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 untuk meluncurkan handler default untuk file yang tidak dapat ditangani aplikasi Anda sendiri.
Mendapatkan 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 suatu file. Opsi ini dijelaskan dalam bagan ini dan di bagian berikut.
Opsi | Metode | Deskripsi |
---|---|---|
Peluncuran default | 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 fallback aplikasi 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 default
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
}
}
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
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 diatur ke true untuk meluncurkan aplikasi yang dipilih pengguna dari kotak dialog Buka Dengan .
Kami menyarankan agar Anda menggunakan 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 pilih 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
}
}
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)
If success Then
' File launched
Else
' File launch failed
End If
Else
' Could not find file
End If
End Sub
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 fallback aplikasi yang direkomendasikan
Dalam beberapa kasus, pengguna mungkin tidak memiliki aplikasi yang diinstal 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, panggil metode Windows.System.Launcher.launchFileAsync(IStorageFile, LauncherOptions) dengan metode LauncherOptions.PreferredApplicationPackageFamilyName diatur ke nama keluarga paket aplikasi di Store yang ingin Anda rekomendasikan. Kemudian, atur LauncherOptions.PreferredApplicationDisplayName ke nama aplikasi tersebut. Windows akan menggunakan informasi ini untuk mengganti opsi umum untuk mencari aplikasi di toko dengan opsi tertentu untuk memperoleh aplikasi yang direkomendasikan dari Toko.
Catatan
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
}
}
async Sub DefaultLaunch()
' Path to the file in the app package to launch
Dim imageFile = "images\test.contoso"
' Get the image file from the package's image directory
Dim file = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync(imageFile)
If file IsNot Nothing Then
' Set the recommended app
Dim options = 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
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
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 Windows)
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 memakan 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 diganti sepenuhnya 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.
Catatan
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 perilaku windowing tertentu untuk aplikasi sumber.
**Keluarga perangkat seluler: **LauncherOptions.DesiredRemainingView tidak didukung pada keluarga perangkat seluler.
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
}
});
}
Keterangan
Aplikasi Anda tidak dapat memilih aplikasi yang diluncurkan. Pengguna menentukan aplikasi mana yang diluncurkan. Pengguna dapat memilih aplikasi Platform Windows Universal (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 file yang diluncurkan 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, .exe, .msi, dan file .js. Pembatasan ini melindungi pengguna dari file yang berpotensi berbahaya yang dapat memodifikasi 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 menjaga skrip dalam file .docx agar tidak memodifikasi 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 mengharapkan bahwa Anda akan mengalami kesalahan ini, kami sarankan Anda memberikan pengalaman fallback kepada pengguna Anda. Misalnya, Anda dapat memberi pengguna opsi untuk menyimpan file ke desktop, dan mereka dapat membukanya di sana.
Topik terkait
Tugas
Panduan
Referensi
Saran dan Komentar
https://aka.ms/ContentUserFeedback.
Segera hadir: Sepanjang tahun 2024 kami akan menghentikan penggunaan GitHub Issues sebagai mekanisme umpan balik untuk konten dan menggantinya dengan sistem umpan balik baru. Untuk mengetahui informasi selengkapnya, lihat:Kirim dan lihat umpan balik untuk