-
Notifications
You must be signed in to change notification settings - Fork 124
DialogFragment service docs #511
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
jfversluis
merged 4 commits into
MicrosoftDocs:main
from
pictos:pj/IdialogFragmentService
Feb 14, 2025
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
117 changes: 117 additions & 0 deletions
117
docs/maui/platform-specific/DialogFragment-customization.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| --- | ||
| title: Dialog Fragment Service - .NET MAUI Community Toolkit | ||
| author: pictos | ||
| 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. | ||
pictos marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ## Usage | ||
|
|
||
| By default the .NET MAUI Community Toolkit register the service, but you can control that, during the builder phase. | ||
pictos marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ```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. | ||
pictos marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ## 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. | ||
pictos marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| 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). | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.