Hello,
Firstly, you need to install CommunityToolkit.Mvvm nuget package. Then use WeakReferenceMessenger to control the full screen or normal screen with native android Apis. You can create two messages like following code in project.
public class FullScreenMessage : ValueChangedMessage<object>
{
public FullScreenMessage(object r) : base(r)
{
}
}
public class NormalScreenMessage: ValueChangedMessage<object>
{
public NormalScreenMessage(object r) : base(r)
{
}
}
When Image page appears, you need to use full screen, when image page disappears, you should back to the normal screen. Please open your pages that contains Images, then override OnAppearing
and OnDisappearing
, send the message like following code.
protected override void OnAppearing()
{
base.OnAppearing();
WeakReferenceMessenger.Default.Send(new FullScreenMessage("HideOsNavigationBar"));
}
protected override void OnDisappearing()
{
base.OnDisappearing();
WeakReferenceMessenger.Default.Send(new NormalScreenMessage("NormalNavigationBar"));
}
In the end, we need to Register the WeakReferenceMessenger, then execute the native android apis to set full screen or normal screen. Please open the MainActivity.cs
(Open Platforms
folder->Android
folder->MainActivity.cs
), then override the OnCreate
method like following code.
public class MainActivity : MauiAppCompatActivity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
WeakReferenceMessenger.Default.Register<FullScreenMessage>(this, (r, m) =>
{
IWindowInsetsController wicController = Window.InsetsController;
Window.SetDecorFitsSystemWindows(false);
Window.SetFlags(WindowManagerFlags.Fullscreen, WindowManagerFlags.Fullscreen);
if (wicController != null)
{
wicController.Hide(WindowInsets.Type.NavigationBars());
}
});
WeakReferenceMessenger.Default.Register<NormalScreenMessage>(this, (r, m) =>
{
IWindowInsetsController wicController = Window.InsetsController;
Window.SetDecorFitsSystemWindows(true);
Window.ClearFlags(WindowManagerFlags.Fullscreen);
if (wicController != null)
{
wicController.Show(WindowInsets.Type.NavigationBars());
}
});
}
}
Best Regards,
Leon Lu
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.