Repository: microsoft/PowerToys
File: src/modules/cmdpal/Microsoft.CmdPal.UI/ExtViews/ContentPage.xaml.cs (C#)

Code before:
    }

    // Using a DependencyProperty as the backing store for ViewModel.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty ViewModelProperty =
        DependencyProperty.Register(nameof(ViewModel), typeof(ContentPageViewModel), typeof(ContentPage), new PropertyMetadata(null));

    public ContentPage()
    {
        this.InitializeComponent();
        this.Unloaded += OnUnloaded;
    }

    private void OnUnloaded(object sender, RoutedEventArgs e)
    {
        // Unhook from everything to ensure nothing can reach us
        // between this point and our complete and utter destruction.
        WeakReferenceMessenger.Default.UnregisterAll(this);
    }

    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        if (e.Parameter is ContentPageViewModel vm)
        {
            ViewModel = vm;
        }
        else if (e.Parameter is NavigateToPageMessage message)
        {
            if (message.CancellationToken.IsCancellationRequested && e.NavigationMode is NavigationMode.New or NavigationMode.Refresh)
            {
                return;
            }

            if (message.Page is ContentPageViewModel lvm2)
            {
                ViewModel = lvm2;
            }
        }

        if (!WeakReferenceMessenger.Default.IsRegistered<ActivateSelectedListItemMessage>(this))
        {
            WeakReferenceMessenger.Default.Register<ActivateSelectedListItemMessage>(this);
        }

        if (!WeakReferenceMessenger.Default.IsRegistered<ActivateSecondaryCommandMessage>(this))
        {
            WeakReferenceMessenger.Default.Register<ActivateSecondaryCommandMessage>(this);
        }

        base.OnNavigatedTo(e);
    }


Review comment (suggestion):
Better replace the message type with something like AsyncNavigationRequest(PageVM, CancellationToken)

Code after:
    }

    // Using a DependencyProperty as the backing store for ViewModel.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty ViewModelProperty =
        DependencyProperty.Register(nameof(ViewModel), typeof(ContentPageViewModel), typeof(ContentPage), new PropertyMetadata(null));

    public ContentPage()
    {
        this.InitializeComponent();
        this.Unloaded += OnUnloaded;
    }

    private void OnUnloaded(object sender, RoutedEventArgs e)
    {
        // Unhook from everything to ensure nothing can reach us
        // between this point and our complete and utter destruction.
        WeakReferenceMessenger.Default.UnregisterAll(this);
    }

    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        if (e.Parameter is not AsyncNavigationRequest navigationRequest)
        {
            throw new InvalidOperationException($"Invalid navigation parameter: {nameof(e.Parameter)} must be {nameof(AsyncNavigationRequest)}");
        }

        if (navigationRequest.TargetViewModel is not ContentPageViewModel contentPageViewModel)
        {
            throw new InvalidOperationException($"Invalid navigation target: AsyncNavigationRequest.{nameof(AsyncNavigationRequest.TargetViewModel)} must be {nameof(ContentPageViewModel)}");
        }

        ViewModel = contentPageViewModel;

        if (!WeakReferenceMessenger.Default.IsRegistered<ActivateSelectedListItemMessage>(this))
        {
            WeakReferenceMessenger.Default.Register<ActivateSelectedListItemMessage>(this);
        }

        if (!WeakReferenceMessenger.Default.IsRegistered<ActivateSecondaryCommandMessage>(this))
        {
            WeakReferenceMessenger.Default.Register<ActivateSecondaryCommandMessage>(this);
        }

        base.OnNavigatedTo(e);
    }

    protected override void OnNavigatingFrom(NavigatingCancelEventArgs e)
    {
        base.OnNavigatingFrom(e);
        WeakReferenceMessenger.Default.Unregister<ActivateSelectedListItemMessage>(this);
        WeakReferenceMessenger.Default.Unregister<ActivateSecondaryCommandMessage>(this);