• Home
  • Documentation
Show / Hide Table of Contents
  • Introduction
  • Commands
    • Commanding
    • Composite Commands
    • Async Commands
    • Error Handling
  • Dependency Injection
    • Getting Started
    • Registering Types
    • Microsoft Extensions (Supplement)
    • Platform Specific Services
    • Exception Handling
    • ContainerLocator
    • Appendix
  • Dialog Service
    • Dialog Service
    • IDialogAware ViewModels
    • IDialogWindow (WPF & Uno Platform)
  • Event Aggregator
  • Mvvm
    • BindableBase
    • ViewModelLocator
  • Modularity
    • Getting Started
    • Module Catalog
    • Module Initialization
  • Navigation
    • Getting Started
    • INavigationParameters
    • Page Navigation
    • Regions
      • Getting Started
      • Region Manager
      • Region Adapters
      • Region Behaviors
      • About Navigation in Prism
      • Basic Region Navigation
      • View/ViewModel Participation
      • Navigating to Existing Views
      • Passing Parameters
      • Confirming Navigation
      • Controlling View Lifetime
      • Navigation Journal
  • Platforms
    • Maui
      • Getting Started
      • Migrating from Prism.Forms
      • PrismAppBuilder
      • AppModel
        • IPageLifecycleAware
      • Behaviors
        • Introduction
        • BehaviorBase<T>
        • EventToCommandBehavior
        • PageBehaviorFactory
      • Dialogs
        • Getting Started
        • IPageDialogService
      • Navigation
        • Introduction
        • Page Navigation
        • NavigationBuilder
        • TabbedPages
        • Understanding the INavigationResult
        • NavigationExceptions
        • Global Navigation Observer
        • XAML Navigation
    • Uno Platform
      • Getting Started
      • Uno.Extensions
    • Wpf
      • Introduction
      • Getting Started
      • View Composition
      • Interactivity
        • Event To Command
    • 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
  • Magician
    • Getting Started
  • Plugins
    • Essentials
      • Getting Started
      • ApplicationModel
        • App Context
        • Browser
        • Communication
          • Email
          • Phone Dialer
        • Data Transfer
          • Clipboard
          • Share
        • LatestVersion
        • Launcher
        • Version Tracking
      • Devices
        • Battery
        • Sensors
          • Biometrics
          • Geocoding
          • Geofencing
          • Geolocation
      • IO
        • File System
        • Stores
      • Media
        • Getting Started
        • Camera
        • Video
      • Networking
        • Connectivity
      • Notifications
        • Getting Started
        • ActionSheets
        • Alerts
        • Prompts
      • Permissions
        • Permissions Manager
      • Threading
        • Main Thread
    • Logging
      • Getting Started
      • Interop
        • Essentials
        • Microsoft Extensions
      • Providers
        • AppCenter
        • Console
        • Debug
        • Firebase
        • Graylog
        • Kochava
        • Raygun
        • Sentry
        • Unit Testing
        • Xunit
    • Observable Regions
    • Popups
  • Pipelines
    • Commercial Plus

BindableBase

Like you would expect from any MVVM Library, Prism provides a base class implementing INotifyPropertyChanged. While it's important to understand how to use it, it's also important to note that features within Prism such as responding to lifecycle events, navigation, etc... are all interface driven. This means that while Prism provides the BindableBase as a base implementation of INotifyPropertyChanged to better assist you, Prism also has no strict requirement on you to use it. This means that you may use any base class you want for your ViewModels including no base class at all (though that isn't generally recommended).

Creating Properties

Properties used within a class inheriting from BindableBase which must notify the UI of changes should make use of the SetProperty method to set the changes and should have both a public property as well as a private backing field. The result is something like this:

public class ViewAViewModel : BindableBase
{
    private string _message;
    public string Message
    {
        get => _message;
        set => SetProperty(ref _message, value);
    }
}

Why Use SetProperty

You may be wondering, why use SetProperty? After all can't you just call RaisePropertyChanged yourself? The short answer is that you can. However this is generally not advisable because you will lose the built in EqualityComparer which helps to ensure that if the setter is called multiple times with the same value INotifyPropertyChanged will only trigger the PropertyChanged event the first time it changes.

public class ViewAViewModel : BindableBase
{
    private string _message;
    public string Message
    {
        get => _message;
        set
        {
            // Don't do this!
            _message = value;
            RaisePropertyChanged();
        }
    }
}
Tip

As we have reviewed code in production, we have commonly run into code like the above sample. This code is fundamentally flawed, overly verbose and will result in unnecessary PropertyChanged events being raised for the property. You should always base your code flow around SetProperty.

Executing a Delegate on PropertyChanges

Sometimes you may want to provide a callback when the property changes. One such example could be that you are implementing IActiveAware and you want to provide a method that will only execute when IsActive is true and another when it is false.

public abstract class ViewModelBase : BindableBase, IActiveAware
{
    private bool _isActive;
    public bool IsActive
    {
        get => _isActive;
        set => SetProperty(ref _isActive, value, () => {
            if (value)
                OnIsActive();
            else
                OnIsNotActive();
        });
    }

    protected virtual void OnIsActive() { }
    protected virtual void OnIsNotActive() { }
}
  • Edit on GitHub
  • Ask questions
  • Follow @PrismLib
  • Follow @BrianLagunas
  • Follow @DanJSiegel
Back to top Copyright 2015-2024 Prism Software, LLC