From 2347861d28028d4d254463e374cde2c589caf780 Mon Sep 17 00:00:00 2001 From: Pedro Jesus Date: Sat, 1 Feb 2025 19:09:31 -0300 Subject: [PATCH 1/3] docs --- .../DialogFragment-customization.md | 117 ++++++++++++++++++ 1 file changed, 117 insertions(+) create mode 100644 docs/maui/platform-specific/DialogFragment-customization.md diff --git a/docs/maui/platform-specific/DialogFragment-customization.md b/docs/maui/platform-specific/DialogFragment-customization.md new file mode 100644 index 00000000..a17f9144 --- /dev/null +++ b/docs/maui/platform-specific/DialogFragment-customization.md @@ -0,0 +1,117 @@ +--- +title: Dialog Fragment Service - .NET MAUI Community Toolkit +author: Pedro Jesus +description: The DialogFragmentService provides an easy way to control the behavior of Modal pages in Android. +ms.date: 02/01/2025 +--- + +# Dialog Fragment Service + +In .NET 9, the .NET MAUI changed how modal pages works in Android, now it uses a [`DialogFragment`](https://developer.android.com/reference/android/app/DialogFragment) and that's a breaking-change. With that some features that worked didn't work anymore and because of that a new way to interact with it has to be implemented. + +The `DialogFragmenteService` is an interface that allows developers to customize the behavior and appearence of Modal pages in .NET MAUI for Android. The .NET MAUI Community Toolkit provides a default implementation to make sure its features, like `StatusBarColor` will work with the new Modal implementation. + +## Usage + +By default the .NET MAUI Community Toolkit register the service, but you can control that, during the builder phase. + +```csharp +public static class MauiProgram +{ + + public static MauiApp CreateMauiApp() + { + var builder = MauiApp.CreateBuilder() + .UseMauiCommunityToolkit(static options => + { + options.SetShouldUseStatusBarBehaviorOnAndroidModalPage(false); + }); + return builder.Build(); + } +} +``` + +Will be cases where you want to fix and customize the `DialogFragment` to match your apps needs, so in for that you can create your own implementation for that and use togheter with other implementations. + +## Create you own service + +Let's say that your app needs to present the Modal in immersive mode, in order to that we need to have access to the `DialogFragment` and configure its `Window` to behave the way we need. + +For that, first we will need to create a class that implements the `IDialogFragmentService` interface and customize the method that we need. And make sure this class will only compile for android, otherwise it will fail to build other targets. + +```csharp +sealed class MyDialogFragmentService : IDialogFragmentService +{ + // Ommiting other methods + + public void OnFragmentStarted(AndroidX.Fragment.App.FragmentManager fm, AndroidX.Fragment.App.Fragment f) + { + if (f is not DialogFragment dialogFragment) + { + return; + } + + if (dialogFragment is not { Dialog.Window: { } window }) + { + return; + } + + if (OperatingSystem.IsAndroidVersionAtLeast(30)) + { + window.SetDecorFitsSystemWindows(false); + window.InsetsController?.Hide(WindowInsets.Type.SystemBars()); + if (window.InsetsController is not null) + { + window.InsetsController.SystemBarsBehavior = (int)WindowInsetsControllerBehavior.ShowTransientBarsBySwipe; + } + } + else + { + SystemUiFlags systemUiVisibility = (SystemUiFlags)window.DecorView.SystemUiVisibility; + systemUiVisibility |= SystemUiFlags.HideNavigation; + systemUiVisibility |= SystemUiFlags.Immersive; + window.DecorView.SystemUiVisibility = (StatusBarVisibility)systemUiVisibility; + } + } +} +``` + +After implementing what is needed it's time to register it into the fragment lifecycle callback, the place to do it is inside the `MauiApp` builder. Here's the code snippet: + +```csharp +public static class MauiProgram +{ + + public static MauiApp CreateMauiApp() + { + var builder = MauiApp.CreateBuilder() + builder.ConfigureLifecycleEvents(static lifecycleBuilder => + { + lifecycleBuilder.AddAndroid(static androidBuilder => + { + androidBuilder.OnCreate(static (activity, _) => + { + if (activity is not AndroidX.AppCompat.App.AppCompatActivity componentActivity) + { + return; + } + + if (componentActivity.GetFragmentManager() is not AndroidX.Fragment.App.FragmentManager fragmentManager) + { + return; + } + + fragmentManager.RegisterFragmentLifecycleCallbacks(new FragmentLifecycleManager(new MyDialogFragmentService()), false); + }); + }); + }); + return builder.Build(); + } +} +``` + +And now, all modals will be showed in full-screen mode. + +## API + +You can find the source code for `IDialogFragmentService` over on the [.NET MAUI Community Toolkit GitHub repository](https://github.com/CommunityToolkit/Maui/blob/main/src/CommunityToolkit.Maui.Core/Interfaces/IDialogFragmentService.android.cs) and about the `FragmentLifecycleManager` over on the [.NET MAUI Community Toolkit GitHub repository](https://github.com/CommunityToolkit/Maui/blob/main/src/CommunityToolkit.Maui.Core/Services/FragmentLifecycleManager.android.cs). From 7c4d79d63b91d905a65cd76957b2057d731340b1 Mon Sep 17 00:00:00 2001 From: Pedro Jesus Date: Sat, 1 Feb 2025 19:55:45 -0300 Subject: [PATCH 2/3] fix author name --- docs/maui/platform-specific/DialogFragment-customization.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/maui/platform-specific/DialogFragment-customization.md b/docs/maui/platform-specific/DialogFragment-customization.md index a17f9144..0e3d307b 100644 --- a/docs/maui/platform-specific/DialogFragment-customization.md +++ b/docs/maui/platform-specific/DialogFragment-customization.md @@ -1,6 +1,6 @@ --- title: Dialog Fragment Service - .NET MAUI Community Toolkit -author: Pedro Jesus +author: pictos description: The DialogFragmentService provides an easy way to control the behavior of Modal pages in Android. ms.date: 02/01/2025 --- From c28874aeb7ed2fd182036f8a8ad036987ea0f624 Mon Sep 17 00:00:00 2001 From: Pedro Jesus Date: Sun, 9 Feb 2025 22:42:37 -0300 Subject: [PATCH 3/3] Text review Co-authored-by: Shaun Lawrence --- .../platform-specific/DialogFragment-customization.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/docs/maui/platform-specific/DialogFragment-customization.md b/docs/maui/platform-specific/DialogFragment-customization.md index 0e3d307b..9d3d84be 100644 --- a/docs/maui/platform-specific/DialogFragment-customization.md +++ b/docs/maui/platform-specific/DialogFragment-customization.md @@ -7,13 +7,13 @@ ms.date: 02/01/2025 # Dialog Fragment Service -In .NET 9, the .NET MAUI changed how modal pages works in Android, now it uses a [`DialogFragment`](https://developer.android.com/reference/android/app/DialogFragment) and that's a breaking-change. With that some features that worked didn't work anymore and because of that a new way to interact with it has to be implemented. +.NET 9.0 introduced a breaking change in how modal pages work in Android - they now use a [`DialogFragment`](https://developer.android.com/reference/android/app/DialogFragment). This breaking change resulted in broken functionality within the .NET MAUI Community Toolkit. The `DialogFragmentService` provides a mechanism to fix the breaking changes, -The `DialogFragmenteService` is an interface that allows developers to customize the behavior and appearence of Modal pages in .NET MAUI for Android. The .NET MAUI Community Toolkit provides a default implementation to make sure its features, like `StatusBarColor` will work with the new Modal implementation. +`IDialogFragmentService` is an interface that allows developers to customize the behavior and appearence of Modal pages in .NET MAUI for Android. The .NET MAUI Community Toolkit provides a default implementation to make sure its features, like `StatusBarColor` will work with the new Modal implementation. ## Usage -By default the .NET MAUI Community Toolkit register the service, but you can control that, during the builder phase. +By default the .NET MAUI Community Toolkit registers the `DialogFragmentService`, but you can control that as follows ```csharp public static class MauiProgram @@ -31,11 +31,11 @@ public static class MauiProgram } ``` -Will be cases where you want to fix and customize the `DialogFragment` to match your apps needs, so in for that you can create your own implementation for that and use togheter with other implementations. +There will be cases where you want to fix and customize the `DialogFragment` to match your apps needs, in these scenarios you can create your own implementation of the `IDialogFragmentService` interface and use in combination with other implementations. ## Create you own service -Let's say that your app needs to present the Modal in immersive mode, in order to that we need to have access to the `DialogFragment` and configure its `Window` to behave the way we need. +The following example shows how to present the modals in immersive mode. In order to achieve this we need to have access to the `DialogFragment` and configure its `Window` to behave in the required way. For that, first we will need to create a class that implements the `IDialogFragmentService` interface and customize the method that we need. And make sure this class will only compile for android, otherwise it will fail to build other targets.