Skip to main content

ON Class

A helper class that allows you to identify device parameters: operating system type, orientation, form factor, and device type (physical device or simulator).

Namespace: DevExpress.Maui.Core

Assembly: DevExpress.Maui.Core.dll

NuGet Package: DevExpress.Maui.Core

Declaration

public static class ON

Remarks

Specify Options Based on Platform

Use the Android and iOS properties to check whether the app is running on an Android or iOS device. The following code shows how to perform platform-specific actions:

using DevExpress.Maui.Core;
//...

    if (ON.Android) {
        // Your custom code here.
    }

    if (ON.iOS) {
        // Your custom code here.
    }

You can also call the Platform method to set values on platforms:

var myValue = ON.Platform(android: 1, iOS: 2);

Specify Options Based on Idiom (Form Factor)

Use the Phone and Tablet properties to check whether the app is running on a phone or a tablet. The following code shows how to perform idiom-specific actions:

using DevExpress.Maui.Core;
//...

    if (ON.Phone) {
        // Your custom code here.
    }

    if (ON.Tablet) {
        // Your custom code here.
    }

You can also call the Idiom method to set values on idioms:

var myValue = ON.Idiom(phone: 1, tablet: 2);

Specify Options Based on Device Type

Use the Simulator property to check whether the app is running on a virtual device:

using DevExpress.Maui.Core;
//...

    if (ON.Simulator) {
        // Your custom code here.
    }

You can also call the DeviceType method to set values on device type:

var myValue = ON.DeviceType(simulator: 1, physical: 2);

Specify Options Based on Screen Orientation

Use the Landscape property to check whether the app is in the Landscape orientation:

using DevExpress.Maui.Core;
//...

    if (ON.Landscape) {
        // Your custom code here.
    }

You can also call the Orientation method to set values depending on the screen orientation:

var myValue = ON.Orientation(portrait: 1, landscape: 2);

Perform Actions When Orientation Changes

Implement a method that is called when the screen orientation changes and pass it to the OrientationChanged method:

class MyControl {
    public MyControl() {
         ON.OrientationChanged(this, static x => x.OnDeviceOrientationChanged());
    }
    void OnDeviceOrientationChanged() {
        // Your custom code here.
    }
}

Closures in the OrientationChanged Handlers

A limitation exists for lambda expressions with outer variables (closures). The event handler refers to a lambda expression with a weak reference. If the garbage collector collects the lambda expression object, the event handler is not invoked.

In the code below, the lambda method refers to the text variable that is defined outside the lambda. In this case, this lambda can be collected and never called.

public class MyControl {
    public MyControl(string text) {
        // WARNING!
        // The lambda may be collected and never called.
        ON.OrientationChanged(this, x => {
            var str = text;
            //...
        });
    }
}

In this case, the lambda expression contains a closure to a local variable that is declared in the subscriber’s constructor. Since the compiler creates an intermediate object for the defined lambda with closures and this intermediate object is weakly referenced, nothing prevents the Garbage Collector from collecting the intermediate object, so your OrientationChanged handler may never be invoked.

To protect a lambda expression object from being collected, declare the text variable as a property at the subscriber object level, for example:

public class MyControl {
    string MyProperty {get; set;}
    public MyControl(string text) {
        MyProperty = text;
        ON.OrientationChanged(this, x => {
            var str = MyProperty;
            //...
        });
    }
}

If your lambda expression does not use outer variables, your handler is invoked without any limitations.

Note

However, we recommend that you mark your delegate as static to avoid closures as described in Perform Actions When Orientation Changes.

Specify Options Based on Screen Size

Call the DisplaySize method to set values depending on the current screen size, regardless of the orientation.

// Example #1
var onDisplaySizeValue1 = ON.DisplaySize<double>(small: 2, large: 4);

// Example #2
var onDisplaySizeValue2 = ON.DisplaySize<double>(extraSmall: 1, small: 2, medium: 3, large: 4, extraLarge: 5, 
    smallThreshold: new Size(360, 640),
    mediumThreshold: new Size(375, 732),
    largeThreshold: new Size(428, 853),
    extraLargeThreshold: new Size(768, 1024));

// Example #3
var onDisplaySizeValue3 = ON.DisplaySize<object>(small: object1, large: object2);

You can also use the OnDisplaySize extension in XAML. For more information, refer to the following help topic: OnDisplaySizeExtension.

Specify Options Based on Screen Width and Height

Call the ScreenWidth and ScreenHeight methods to set values depending on the current screen width or height:

// Example #1
var onWidthValue1 = ON.ScreenWidth<object>(small: object1, large: object2);

// Example #2
var onWidthValue2 = ON.ScreenWidth<double>(extraSmall: 1, small: 2, medium: 3, large: 4, extraLarge: 5,
    smallThreshold: 360,
    mediumThreshold: 375,
    largeThreshold: 428,
    extraLargeThreshold: 768);

// Example #3
var onHeightValue1 = ON.ScreenHeight<object>(small: object1, large: object2);

// Example #4
var onHeightValue2 = ON.ScreenHeight<double>(extraSmall: 1, small: 2, medium: 3, large: 4, extraLarge: 5,
    smallThreshold: 640,
    mediumThreshold: 732,
    largeThreshold: 853,
    extraLargeThreshold: 1024);

You can also use the OnScreenWidth and OnScreenHeight extensions in XAML. For more information, refer to the following help topics:

Inheritance

See Also