官术网_书友最值得收藏!

Platform extensions

As we mentioned previously, UWP supports a wide range of devices. Each of these devices executes its own implementation of .NET Standard and the UWP app model.

Nevertheless, the surface area of this complete API layer might not always apply to the target platform. The UWP app model contains certain APIs that are specific to only a subset of these devices. These types of API modules are in fact left as placeholder methods in the core UWP SDK, while the actual implementation is included in extension modules that can be referenced in your UWP applications:

Without adding the specific SDK, the developers are confined to only universal APIs. Without adding the extension modules, it is highly likely that certain platform-specific methods would throw NotImplementedException or similar, since the actual implementation of these methods only exists in the platform extensions libraries.

After including the target platform extension, the developers are also responsible for executing runtime checks for the methods and events to see whether these APIs are supported in the current device runtime. Developers can make use of various ApiInformation methods, such as IsTypePresent, IsEventPresent, IsMethodPresent, and IsPropertyPresent.

For instance, in order to check whether the current device supports the CameraPressed event (it might be present on a mobile device, though unlikely to be supported on a desktop PC), we would need to resort to IsEventPresent:

 bool isHardwareButtons_CameraPressedAPIPresent = Windows.Foundation.Metadata.ApiInformation.IsEventPresent ("Windows.Phone.UI.Input.HardwareButtons", "CameraPressed");

This runtime check can also be executed at the contract level to see whether a group of events, or other class members that are used to execute a certain action, are supported or not:

 bool isWindows_Devices_Scanners_ScannerDeviceContract_1_0Present = Windows.Foundation.Metadata.ApiInformation.IsApiContractPresent ("Windows.Devices.Scanners.ScannerDeviceContract", 1, 0);

This way, developers can avoid situations where the application behavior and compliance with requirements is neither predetermined nor predictable.

主站蜘蛛池模板: 永新县| 文登市| 乌兰县| 台州市| 邓州市| 长春市| 黎城县| 镇沅| 岢岚县| 安宁市| 贵州省| 新泰市| 山阴县| 高安市| 尖扎县| 临武县| 锡林郭勒盟| 游戏| 锦屏县| 宁陵县| 巨鹿县| 巩义市| 二连浩特市| 夏河县| 河南省| 彭水| 含山县| 陵川县| 临安市| 望城县| 静宁县| 文化| 莱阳市| 北流市| 九龙县| 柳林县| 牙克石市| 丹江口市| 德格县| 进贤县| 通化县|