|
| 1 | +// Copyright (c) 2023 .NET Foundation and Contributors. All rights reserved. |
| 2 | +// Licensed to the .NET Foundation under one or more agreements. |
| 3 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 4 | +// See the LICENSE file in the project root for full license information. |
| 5 | + |
| 6 | +using Microsoft.Maui.Controls; |
| 7 | + |
| 8 | +namespace ReactiveUI.Maui; |
| 9 | + |
| 10 | +/// <summary> |
| 11 | +/// Reactive Flyout Page. |
| 12 | +/// </summary> |
| 13 | +/// <typeparam name="TViewModel">The type of the view model.</typeparam> |
| 14 | +/// <seealso cref="FlyoutPage" /> |
| 15 | +/// <seealso cref="IViewFor{TViewModel}" /> |
| 16 | +public class ReactiveFlyoutPage<TViewModel> : FlyoutPage, IViewFor<TViewModel> |
| 17 | + where TViewModel : class |
| 18 | +{ |
| 19 | + /// <summary> |
| 20 | + /// The view model bindable property. |
| 21 | + /// </summary> |
| 22 | + public static readonly BindableProperty ViewModelProperty = BindableProperty.Create( |
| 23 | + nameof(ViewModel), |
| 24 | + typeof(TViewModel), |
| 25 | + typeof(ReactiveFlyoutPage<TViewModel>), |
| 26 | + default(TViewModel), |
| 27 | + BindingMode.OneWay, |
| 28 | + propertyChanged: OnViewModelChanged); |
| 29 | + |
| 30 | + /// <summary> |
| 31 | + /// Gets or sets the ViewModel to display. |
| 32 | + /// </summary> |
| 33 | + public TViewModel? ViewModel |
| 34 | + { |
| 35 | + get => (TViewModel)GetValue(ViewModelProperty); |
| 36 | + set => SetValue(ViewModelProperty, value); |
| 37 | + } |
| 38 | + |
| 39 | + /// <inheritdoc/> |
| 40 | + object? IViewFor.ViewModel |
| 41 | + { |
| 42 | + get => ViewModel; |
| 43 | + set => ViewModel = (TViewModel?)value; |
| 44 | + } |
| 45 | + |
| 46 | + /// <inheritdoc/> |
| 47 | + protected override void OnBindingContextChanged() |
| 48 | + { |
| 49 | + base.OnBindingContextChanged(); |
| 50 | + ViewModel = BindingContext as TViewModel; |
| 51 | + } |
| 52 | + |
| 53 | + private static void OnViewModelChanged(BindableObject bindableObject, object oldValue, object newValue) => bindableObject.BindingContext = newValue; |
| 54 | +} |
0 commit comments