• Home
  • Documentation
Show / Hide Table of Contents
  • Introduction
  • Getting Started
    • Download and Setup Prism
    • NuGet Packages
    • Productivity Tools
  • Commands
    • Commanding
    • Composite Commands
  • Dependency Injection
    • Getting Started
    • Registering Types
    • Platform Specific Services
    • Exception Handling
    • ContainerLocator
    • Adding a Custom Container
    • Appendix
  • Event Aggregator
  • ViewModelLocator
  • Modules
  • WPF / Uno
    • Introduction
    • Getting Started
    • Converting From Prism 7.x
    • Converting From Prism 6.x
    • View Composition
    • Region Navigation
      • About Navigation in Prism
      • Basic Region Navigation
      • View/ViewModel Participation
      • Navigating to Existing Views
      • Passing Parameters
      • Confirming Navigation
      • Controlling View Lifetime
      • Navigation Journal
    • Interactivity
      • Event To Command
    • Dialog Service
    • Advanced
      • Region Adapters
    • Legacy (Prism 6)
      • Introduction
      • Initializing
      • Managing-Dependencies
      • Modules
      • Implementing-MVVM
      • Advanced-MVVM
      • Composing-the-UI
      • Navigation
      • Communication
      • Deploying
      • Appendix-A-Glossary
      • Appendix-B-Patterns
      • Appendix-C-Prism-Library
      • Appendix-D-Extending-Prism
      • Appendix-E-Click-Once
  • .NET MAUI
    • Getting Started
    • Migrating from Prism.Forms
    • PrismAppBuilder
    • Dependency Injection
    • AppModel
      • IPageLifecycleAware
    • Behaviors
      • Introduction
      • BehaviorBase<T>
      • EventToCommandBehavior
      • PageBehaviorFactory
    • Dialogs
      • Getting Started
      • IPageDialogService
      • IDialogService
    • Navigation
      • Introduction
      • Page Navigation
      • NavigationBuilder
      • Understanding the INavigationResult
      • NavigationExceptions
      • Global Navigation Observer
      • XAML Navigation
    • Regions
      • Introduction
  • Xamarin.Forms
    • Create Your First App
    • Behaviors
      • Working with Behaviors
      • EventToCommand Behavior
      • PageBehaviorFactory
    • Dialogs
      • Dialogs
      • Page Dialog Service
      • Dialog Service
      • Styling Dialogs
    • Navigation
      • Navigation Basics
      • Passing Parameters
      • Confirming Navigation
      • Deep Linking
      • Working w/ MasterDetailPages
      • Working w/ NavigationPages
      • Working w/ TabbedPages
      • XAML Navigation
    • Application Lifecycle
    • Page Lifecycle
    • Additional Platforms
      • GTK

Page Behavior Factory

There are many times where you may want to apply specific behaviors to pages universally. Internally Prism utilizes the IPageBehaviorFactory to apply Behaviors for supporting IActiveAware on the children of the TabbedPage CarouselPage, & NavigationPage as well as handling IPageLifecycleAware. In the event that there are custom behaviors which you would like to apply you can provide a custom implementation of the PageBehaviorFactory.

While this is generally utilized for applying Behaviors, we can do anything we may need to here to configure our Page. As an example we could simplify our Pages, and apply some Platform Specific's from the PageBehaviorFactory. For an example let's assume that we want the Tabbed Bar on the bottom for Android and we want to use the Safe Area on iOS. Given this we could create the following CustomPageBehaviorFactory:

using Prism.Behaviors;
using Xamarin.Forms;
using Xamarin.Forms.PlatformConfiguration;
using Xamarin.Forms.PlatformConfiguration.AndroidSpecific;
using Xamarin.Forms.PlatformConfiguration.iOSSpecific;
using TabbedPage = Xamarin.Forms.TabbedPage;

namespace MyApp.Behaviors
{
    public class CustomPageBehaviorFactory : PageBehaviorFactory
    {
        public override void ApplyContentPageBehaviors(ContentPage page)
        {
            base.ApplyContentPageBehaviors(page);
            page.On<iOS>().SetUseSafeArea(true);
        }

        public override void ApplyTabbedPageBehaviors(TabbedPage page)
        {
            base.ApplyTabbedPageBehaviors(page);
            page.On<Android>().SetToolbarPlacement(ToolbarPlacement.Bottom);
        }
    }
}

Now that we have a custom implementation all we need to do is Register it in our PrismApplication and we will get the custom behavior and configuration that we are looking for.

protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
    constinerRegistry.RegisterSingleton<IPageBehaviorFactory, CustomPageBehaviorFactory>();
}
Warning

Prism 8 will have some breaking changes here. When this API was first introduced IPageBehaviorFactory was given several Page type specific methods. These aren't actually used anywhere. For this reason the interface will be simplified to only declare the single ApplyPageBehaviors(Page) method. All of the methods in the PageBehaviorFactory will be changed from public to protected and will remain virtual so that you can apply your custom behaviors.

  • Edit on GitHub
  • Ask questions
  • Follow @PrismLib
  • Follow @BrianLagunas
  • Follow @DanJSiegel
Back to top Copyright 2015-2022 Prism