|
| 1 | +// Copyright (c) 2022 .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 System; |
| 7 | +using Microsoft.Maui.Controls; |
| 8 | +using Splat; |
| 9 | + |
| 10 | +namespace ReactiveUI.Maui; |
| 11 | + |
| 12 | +/// <summary> |
| 13 | +/// ReactiveShellContent. |
| 14 | +/// </summary> |
| 15 | +/// <typeparam name="TViewModel">The type of the view model.</typeparam> |
| 16 | +/// <seealso cref="Microsoft.Maui.Controls.ShellContent" /> |
| 17 | +/// <seealso cref="ReactiveUI.IActivatableView" /> |
| 18 | +public class ReactiveShellContent<TViewModel> : ShellContent, IActivatableView |
| 19 | + where TViewModel : class |
| 20 | +{ |
| 21 | + /// <summary> |
| 22 | + /// The contract property. |
| 23 | + /// </summary> |
| 24 | + public static readonly BindableProperty ContractProperty = BindableProperty.Create( |
| 25 | + nameof(Contract), |
| 26 | + typeof(string), |
| 27 | + typeof(ReactiveShellContent<TViewModel>), |
| 28 | + null, |
| 29 | + BindingMode.Default, |
| 30 | + propertyChanged: ViewModelChanged); |
| 31 | + |
| 32 | + /// <summary> |
| 33 | + /// The view model property. |
| 34 | + /// </summary> |
| 35 | + public static readonly BindableProperty ViewModelProperty = BindableProperty.Create( |
| 36 | + nameof(ViewModel), |
| 37 | + typeof(TViewModel), |
| 38 | + typeof(ReactiveShellContent<TViewModel>), |
| 39 | + default(TViewModel), |
| 40 | + BindingMode.Default, |
| 41 | + propertyChanged: ViewModelChanged); |
| 42 | + |
| 43 | + /// <summary> |
| 44 | + /// Initializes a new instance of the <see cref="ReactiveShellContent{TViewModel}" /> class. |
| 45 | + /// </summary> |
| 46 | + public ReactiveShellContent() |
| 47 | + { |
| 48 | + var view = Locator.Current.GetService<IViewFor<TViewModel>>(Contract); |
| 49 | + if (view is not null) |
| 50 | + { |
| 51 | + ContentTemplate = new DataTemplate(() => view); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + /// <summary> |
| 56 | + /// Gets or sets the view model. |
| 57 | + /// </summary> |
| 58 | + /// <value> |
| 59 | + /// The view model. |
| 60 | + /// </value> |
| 61 | + public TViewModel? ViewModel |
| 62 | + { |
| 63 | + get => (TViewModel)GetValue(ViewModelProperty); |
| 64 | + set => SetValue(ViewModelProperty, value); |
| 65 | + } |
| 66 | + |
| 67 | + /// <summary> |
| 68 | + /// Gets or sets the contract for the view. |
| 69 | + /// </summary> |
| 70 | + /// <value> |
| 71 | + /// The contract. |
| 72 | + /// </value> |
| 73 | + public string? Contract |
| 74 | + { |
| 75 | + get => (string?)GetValue(ContractProperty); |
| 76 | + set => SetValue(ContractProperty, value); |
| 77 | + } |
| 78 | + |
| 79 | + private static void ViewModelChanged(BindableObject bindable, object oldValue, object newValue) |
| 80 | + { |
| 81 | + if (Locator.Current is null) |
| 82 | + { |
| 83 | + throw new NullReferenceException(nameof(Locator.Current)); |
| 84 | + } |
| 85 | + |
| 86 | + if (bindable is ReactiveShellContent<TViewModel> svm) |
| 87 | + { |
| 88 | + var view = Locator.Current.GetService<IViewFor<TViewModel>>(svm.Contract); |
| 89 | + if (view is not null) |
| 90 | + { |
| 91 | + svm.ContentTemplate = new DataTemplate(() => view); |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | +} |
0 commit comments