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

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.

主站蜘蛛池模板: 隆昌县| 北安市| 昂仁县| 三江| 江达县| 汉阴县| 壤塘县| 蓬莱市| 忻州市| 宜良县| 津南区| 湄潭县| 通道| 新巴尔虎右旗| 科尔| 马鞍山市| 锡林郭勒盟| 青海省| 高淳县| 禹城市| 蓬溪县| 绿春县| 汉阴县| 昌黎县| 辽源市| 仁布县| 工布江达县| 广宗县| 普洱| 保定市| 陇南市| 伊金霍洛旗| 兴安盟| 承德市| 错那县| 临安市| 太原市| 浑源县| 湾仔区| 慈利县| 德惠市|