Reputation: 2309
I've created a UWP app and a UWP optional package. I got that all set up using this documentation: https://learn.microsoft.com/en-us/windows/uwp/packaging/optional-packages-with-executable-code
Everything is working as expected and I can use code in the optional package. But obviously if I remove the optional package, my code won't work. I don't want to have my app crash if the "optional" package isn't installed.
What is the accepted way of checking if an optional package is installed? Do I just catch the exception? That seems a bit hacky.
Example: When I reference Class1
(in my optional package) it throws this exception:
System.Runtime.InteropServices.COMException: 'Class not registered (Exception from HRESULT: 0x80040154)'
Upvotes: 4
Views: 796
Reputation: 32785
What is the accepted way of checking if an optional package is installed?
You could use PackageCatalog
class to get all the packages installed.
List<string> optionalPackageList = new List<string>();
foreach (var package in Windows.ApplicationModel.Package.Current.Dependencies)
{
if (package.IsOptional)
{
optionalPackageList.Add(package.Id.FamilyName);
}
}
You could check if the optionalPackageList
has contain the specified option package . For more please refer this document.
Upvotes: 3