Thursday, July 4, 2024

Disable Dark mode in .NET Maui

Some applications need to be in Light mode even the device is in Dark mode, in that cases we have to override the Dark mode feature and display only the Light mode in our applications. There are many ways to do that, here are the possible ways to disable Dark mode.

Use the below code snippet in App.xaml.cs file in the constructor:

Application.Current.UserAppTheme = AppTheme.Light;

If this doesn't work then use the platform specific code to disable the Dark mode.

Dark mode in iOS

Add the following key into the Info.plist in Platform/iOS folder

<key>UIUserInterfaceStyle</key>
<string>Light</string>

Dark mode in Android

In MainActivity.cs add the following into the OnCreate method:

protected override void OnCreate(Bundle? savedInstanceState)
{
base.OnCreate(savedInstanceState);
//For Android API under 31
AppCompatDelegate.DefaultNightMode = AppCompatDelegate.ModeNightNo;
//For Android API 31+
var uiModeManager = (UiModeManager)GetSystemService(UiModeService);
uiModeManager.SetApplicationNightMode(1);
}

Saturday, March 16, 2024

Rounded editor field using handlers in .NET Maui

Customizing the UI elements is challenging in app development when we are using cross platform mobile app development. UIs will be different in both the platforms but as a cross platform app developer they have to write a custom code to match the UI elements in both the platforms.

This customization was implemented using Custom Renders in Xamarin.Forms framework. Now in .NET Maui they are called as handlers. There are many was to write handlers in Maui, for implementing rounded editor we use a  custom controls.Handlers are global, to customize a specific Editor control on a page that contains multiple Editor controls, you should first subclass the Editor control.

CustomEditor
After creating the mappers for your handler, you must provide handler implementations on all platforms. This can be accomplished by adding partial class handler implementations in the child folders of the Platforms folder.

Create the platform controls in Android

CustomEditorMapper

Create the platform controls in iOS

CustomEditorMapper
Register the handler

A custom control and its handler must be registered with an app, before it can be consumed. This should occur in the CreateMauiApp method in the MauiProgram class in your app project, which is the cross-platform entry point for the app:
MauiProgram

Consuming custom controls in XAML file:

MainPage.xaml

Final outcome of this implementation is here:



Complete project can be found here: https://github.com/GovardhanNag/EditorRenderer.git

Saturday, February 24, 2024

Rounded entry field using handlers in .NET Maui

Customizing the UI elements is challenging in app development when we are using cross platform mobile app development. UIs will be different in both the platforms but as a cross platform app developer they have to write a custom code to match the UI elements in both the platforms.

This customization was implemented using Custom Renders in Xamarin.Forms framework. Now in .NET Maui they are called as handlers. There are many ways to write handlers in Maui, for implementing rounded entry we use a  customized control approach. Handlers are global, to customize a specific Entry control on a page that contains multiple Entry controls, you should first subclass the Entry control.

You can then customize the EntryHandler, via its property mapper, to perform the desired modification only to CustomEntry instances:

If the handler customization is performed in your App class, any CustomEntry instances in the app will be customized as per the handler modification.
Consuming custom controls in XAML file:

Final outcome of this implementation is here:

Complete project can be found here: https://github.com/GovardhanNag/EditorRenderer.git


Saturday, February 17, 2024

Device ID from iOS and Android platforms using .Net MAUI

In most of the scenarios app developers require unique device Ids to identify the duplicasy of their apps, or to encrypt their data with salt key as device Ids. So to get these device Id's we have to write a platform specific code in .NET Maui. In Xamarin.Forms we rely on Dependency Services to write the platform specific code, but in .NET Maui this can be achieved by creating partial classes. Here I'll explain step by step procedure for obtaining device Id's from partial class.

Cross platform partial class

Create a partial class and partial method, that will be implemented in platform specific partial classes.

 

GetDeviceInfo - Cross platform

With the GetDeviceID method we should obtain device Id's from each platform. 
Note: all the partial classes should have the same namespace, even though they are created in different folders, their namespace should match each other.

Android partial class 

Navigate to Platform\Android folder in the project solution and create another partial class.

iOS partial class 

Navigate to Platform\iOS folder in the project solution and create another partial class.

Here is how we call the platform specific partial method in our MainPage.xaml.cs file.

 
Finally the outcome of this implementation is here:
iOS Device id
Android Device Id




















SSO login using Launcher in .NET MAUI

  Some of the scenarios require to open the external/system browser to perform oAuth 2.0 login and then receive the auth code via redirect U...