| | | 1 | | using Prism; |
| | | 2 | | using Prism.Navigation; |
| | | 3 | | using Xamarin.CommunityToolkit.UI.Views; |
| | | 4 | | using Xamarin.Forms; |
| | | 5 | | |
| | | 6 | | namespace ZhEaIsNsAaBn.Xamarin; |
| | | 7 | | |
| | | 8 | | /// <summary> |
| | | 9 | | /// A Xamarin.CommunityToolkit lazy view that resolves <typeparamref name="TView"/> through Prism, so the view gets its |
| | | 10 | | /// Prism view model (and regions work), then passes <see cref="NavigationParameters"/> to that view model. |
| | | 11 | | /// </summary> |
| | | 12 | | /// <typeparam name="TView">The view to load lazily.</typeparam> |
| | | 13 | | public class PrismLazyView< TView > : BaseLazyView |
| | | 14 | | where TView : View, new() |
| | | 15 | | { |
| | | 16 | | /// <summary>Bindable property for <see cref="NavigationParameters"/> (Xamarin.Forms convention: public static field |
| | 3 | 17 | | public static readonly BindableProperty NavigationParametersProperty = BindableProperty.Create( nameof( NavigationPa |
| | 3 | 18 | | typeof( INavigation |
| | 3 | 19 | | typeof( PrismLazyVi |
| | 3 | 20 | | propertyChanged: On |
| | | 21 | | |
| | | 22 | | |
| | | 23 | | /// <summary> |
| | | 24 | | /// Parameters for the view model (<see cref="IPrismLazyViewModel.LazyLoadInitialize"/>). Passed after the view load |
| | | 25 | | /// again on every change, including changes through a binding. Default: none (an empty set is passed). |
| | | 26 | | /// </summary> |
| | | 27 | | public INavigationParameters? NavigationParameters |
| | | 28 | | { |
| | 12 | 29 | | get => (INavigationParameters?)GetValue( NavigationParametersProperty ); |
| | 3 | 30 | | set => SetValue( NavigationParametersProperty, value ); |
| | | 31 | | } |
| | | 32 | | |
| | | 33 | | |
| | | 34 | | /// <summary> |
| | | 35 | | /// Creates the view with <see cref="CreateView"/>, shows it, marks the view as loaded, then calls |
| | | 36 | | /// <see cref="IPrismLazyViewModel.LazyLoadInitialize"/> if the view's binding context implements it. |
| | | 37 | | /// </summary> |
| | | 38 | | /// <returns>A completed task.</returns> |
| | | 39 | | /// <exception cref="InvalidOperationException">The app is not a Prism application (see <see cref="CreateView"/>).</ |
| | | 40 | | public override ValueTask LoadViewAsync() |
| | | 41 | | { |
| | 15 | 42 | | Content = CreateView(); |
| | | 43 | | |
| | 12 | 44 | | SetIsLoaded( true ); |
| | 12 | 45 | | InitializeViewModel(); |
| | | 46 | | |
| | 12 | 47 | | return default; |
| | | 48 | | } |
| | | 49 | | |
| | | 50 | | /// <summary>Creates the view. Default: resolves <typeparamref name="TView"/> from the Prism container.</summary> |
| | | 51 | | /// <returns>The view to show.</returns> |
| | | 52 | | /// <exception cref="InvalidOperationException"><see cref="Application.Current"/> is not a <see cref="PrismApplicati |
| | | 53 | | protected virtual TView CreateView() |
| | | 54 | | { |
| | 3 | 55 | | if ( Application.Current is not PrismApplicationBase application ) |
| | | 56 | | { |
| | 3 | 57 | | throw new InvalidOperationException( "PrismLazyView needs a Prism application: Application.Current must deri |
| | | 58 | | } |
| | | 59 | | |
| | | 60 | | |
| | 0 | 61 | | return (TView)application.Container.Resolve( typeof( TView ) ); |
| | | 62 | | } |
| | | 63 | | |
| | | 64 | | |
| | | 65 | | private static void OnNavigationParametersChanged( BindableObject bindable, object? oldValue, object? newValue ) |
| | 6 | 66 | | => ( (PrismLazyView<TView>)bindable ).InitializeViewModel(); |
| | | 67 | | |
| | | 68 | | private void InitializeViewModel() |
| | | 69 | | { |
| | 18 | 70 | | if ( IsLoaded && Content?.BindingContext is IPrismLazyViewModel viewModel ) |
| | | 71 | | { |
| | 12 | 72 | | viewModel.LazyLoadInitialize( NavigationParameters ?? new NavigationParameters() ); |
| | | 73 | | } |
| | 18 | 74 | | } |
| | | 75 | | } |