

The folders for each target platform contain platform-specific code that starts the app on each platform, plus any additional platform code you add. NET MAUI app project contains a Platforms folder, with each child folder representing a platform that. Using ĭeviceOrientationService deviceOrientationService = new DeviceOrientationService() ĭeviceOrientation orientation = deviceOrientationService.GetOrientation() Īt build time the build system uses conditional compilation to target Android and iOS platform code to the correct platform.įor more information about conditional compilation, see Conditional compilation. The DeviceOrientationService.GetOrientation method can then be invoked from cross-platform code by creating an object instance and invoking its operation: using InvokePlatformCodeDemos.Services

Alternatively, rather than returning DeviceOrientation.Undefined you could throw a PlatformNotSupportedException that specifies the platforms that implementations are provided for: throw new PlatformNotSupportedException("GetOrientation is only supported on Android and iOS.") On other platforms, DeviceOrientation.Undefined is returned. In this example, platform implementations of the GetOrientation method are provided for Android and iOS.

Return isPortrait ? DeviceOrientation.Portrait : DeviceOrientation.Landscape UIInterfaceOrientation orientation = īool isPortrait = orientation = UIInterfaceOrientation.Portrait || orientation = UIInterfaceOrientation.PortraitUpsideDown Return isLandscape ? DeviceOrientation.Landscape : DeviceOrientation.Portrait SurfaceOrientation orientation = īool isLandscape = orientation = SurfaceOrientation.Rotation90 || orientation = SurfaceOrientation.Rotation270 IWindowManager windowManager = .GetSystemService(Context.WindowService).JavaCast() Public DeviceOrientation GetOrientation() This can be accomplished by writing a method that uses conditional compilation to target different platforms: #if ANDROID Retrieving the orientation of your device requires writing platform code. The following example shows the DeviceOrientation enumeration, which will be used to specify the orientation of your device: namespace InvokePlatformCodeDemos.Services Platform code can be invoked from cross-platform code by using conditional compilation to target different platforms. Platform code can be invoked from cross-platform code by using conditional compilation, or by using partial classes and partial methods. This requires knowledge of Apple's iOS and MacCatalyst APIs, Google's Android APIs, and Microsoft's Windows App SDK APIs. NET Multi-platform App UI (.NET MAUI) doesn't provide any APIs for accessing specific platform APIs, you can write your own code to access the required platform APIs.
